I have 3 models A, B, C
A belongs to B B belongs to C
I want to query A, include B, include C. But I only want a few fields from each model. Using a filter like this without specifying any "fields" works fine and brings back the nested models.
{
"include": {
"relation": "modelB",
"scope": {
"include": {
"relation": "modelC"
}
}
}
}
But as soon as I add a "fields" filter property to any level of the query, all included relations from that level seem to be ignored. So if I queried with this filter:
{
"fields": ["modelAField"],
"include": {
"relation": "modelB",
"scope": {
"include": {
"relation": "modelC"
}
}
}
}
I only get modelA instances with modelAField - as if I didn't ask for modelB to be included at all. If I omit "fields" at the top level and add it to the modelB scope, then I get all fields of modelA, the subset of modelB fields I ask for, but no inclusion of modelC.
I walked through the loopback-connector code and it looks like what's happening is that because I didn't include the foreign key field in my "fields" array, the initial query didn't bring back enough information to correctly write the query to bring back the related objects.
tl:dr
Include the foreign key in all the relations.
Source: https://github.com/strongloop/loopback/issues/2186 I included this, because I think many people when programming don't go to github triage or bug issues to find a solution for their problem but to SO. That's why I think it's usefull to have this question here posted.