Sails.js: how to Join multiple models using waterline?

核能气质少年 提交于 2019-12-14 01:19:14

问题


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

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