3

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);
 });
Joe Hill
  • 333
  • 3
  • 12
Arun Febi
  • 119
  • 9
  • 2
    So you have gotten as far as creating three models with associations to each other, including a model (`Country`) that has associations to _both_ other models. Are you really stumped about how to write a Waterline query that will link all the results together, or have you just not really tried? I'll give you a hint: you can call `populate` multiple times, for different associations. – sgress454 Oct 12 '15 at 19:58
  • The snark is unnecessary, but @sgress454 is right. RTFM and use populate for each model or `populateAll()` – coagmano Oct 13 '15 at 03:29
  • Thank you for your comments. Yes I know that I can join the table with populate(). but I need something like this https://github.com/balderdashy/waterline/issues/112 Country.join(Continent,cb); – Arun Febi Oct 13 '15 at 04:54
  • any update on this? [this link](https://github.com/treelinehq/waterline-sql-builder/blob/master/docs/syntax.md#join-methods) provides some documentation about joins but not sure whether they are available and how to use them – Raza Ahmed Nov 18 '16 at 15:25

0 Answers0