How to do where condition on include relation in Loopback

I want to get the result with include relation with where condition on include model.

return this.htcApi.find({   include: [     {       relation: 'nmos',       scope: {         include: 'countries',       },     },   ],   where: { name:'Welcome', "nmos.name":'agile'} }); 

This where is condition work for name of htc model not for noms module.

I want query like

Select htc.*, nmos.* FROM htc LEFT JOIN nmos ON nmos.id = htc.n_id where htc.name = 'abc' and nmos.name = 'abc'; 

How can add where condition on the "relation" table?

Add Comment
1 Answer(s)

In your query, you just need to add the property where within the scope property, like this:

return this.htcApi.find({   include: [     {       relation: 'nmos',       scope: {         include: 'countries',         where: {           and [             { id: htc.n_id },             { name: 'abc' },           ],         },       },     },   ],   where: { name: 'abc' } }); 

This should return the htcApi objects named ‘abc’ with the related nmos objects that have the name ‘abc’ and the id ‘n_id’.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.