问题
I've 3 models Continent,Country and city. I'd like to join these models to get the results.
Continent.js
attributes: {
continent_Id:{
type:'string',
primaryKey: true
},
continent_Name:{
type:'string'
},
description:{
type:'string'
},
country:{
collection: 'country',
via: 'continent'
}
}
Country.js
attributes: {
country_Id:{
type:'string',
primaryKey: true
},
countryName:{
type:'string'
},
description:{
type:'string'
},
continent:{
model:'continent'
},
languages:{
collection:'Country_language',
via:'country'
},
city:{
collection:'city',
via:'country'
}
}
city.js
attributes: {
city_Id:{
type:'string',
primaryKey:true
},
cityName:{
type:'string'
},
description:{
type:'string'
},
country:{
model:'country'
}
Now I can get the results by using Model.query :
var query = 'select a.continent_Name,b.countryName,c.cityName from continent a inner join country b on a.continent_id = b.continent inner join city c on b.country_Id = c.country';
Continent.query(query,function(err,data){
console.log(data);
});
and the result is:
[ { continent_Name: 'ASIA', countryName: 'India', cityName: 'Delhi' } ]
Is there any way to get the same results by using 'waterline' query like
Continent.find().populate().where({}).exec(function(err,data){
console.log(data);
});
来源:https://stackoverflow.com/questions/33078929/sails-js-how-to-join-multiple-models-using-waterline