问题
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