Sails js, how do we write inner join using populate

不羁岁月 提交于 2019-12-24 06:36:43

问题


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.


回答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'
}]
}


来源:https://stackoverflow.com/questions/37790797/sails-js-how-do-we-write-inner-join-using-populate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!