1

Sails's populate works as Left join. I want to do as inner join,

Anybody help me how we can write inner join using populate without writing raw query. I am using MYSQL adapter.

Muthukumar Marichamy
  • 1,175
  • 1
  • 17
  • 39

1 Answers1

1

if you have for example two models user , contacts relation one-to-many you try to write something like :-

first models will like this :-

user.js

module.exports = {
  schema: true,
  tableName: 'userdata',
  attributes: {
    anyField: {
      type: 'integer',
      required: false,
      size: 56,
    },
    contacts: {
      collection: 'Contact',
      via: 'user'
    }

};

contact.js :-

module.exports = {

  attributes: {
    name: {
      type: 'string'
    },
        mobile_number: {
      type: 'string'
    },

    user: {
      model: 'User'
    }
};

and you can get result of populate like this :-

User.find(user.id).populate('contacts').exec(function (err, user) {
  // this contains  user object  and populated by contacts 
     console.log(user[0].contacts);
});

you get result like :-

{
name:'tets',
contacts:[{
 'name':'test',
'mobile_number':'00000'
},
{
 'name':'test',
'mobile_number':'00000'
},
{
 'name':'test',
'mobile_number':'00000'
}]
}
Mohamed Elfiky
  • 106
  • 1
  • 7
Ahmed farag mostafa
  • 2,802
  • 2
  • 14
  • 33
  • Hi Mohamed, Do you know how to work this when you have an existing DB? I have an open question here you may be able to answer: http://stackoverflow.com/questions/37742970/waterline-js-joins-populate-with-existing-database – munkee Jun 15 '16 at 07:21
  • 3
    That's not an inner join but a left join. It will also return users which does not have any contact. As fas as I know inner join is currently not possible using waterline.js, which is the default sails.js ORM. – jelhan Sep 23 '16 at 10:13