Search code examples
postgresqlloopback

Query 3 Deep Relationship Loopback


I'm trying to do a nested query as per the instructions and examples here: https://docs.strongloop.com/display/public/LB/Include+filter https://github.com/strongloop/loopback/issues/735

but it is not working like I expected it to. I have an Account, that can have many Branches, that can have many Incidents, Risk Assessments and Claims. I have created a custom endpoint for the Branch to query the related Incidents, Risk Assessments, and Claims, but I can't get the information from the Account level.

Here is the working code for the Branch:

Branch.find({
        where: {
            id: id
        },
        include: [
            {
                relation: 'incidents',
                scope: {
                    where: {
                        incidentDate: {
                            between: [
                                beginDate,
                                today
                            ]
                        }
                    }
                }
            },
            { relation: 'riskAssessments' },
            { relation: 'claims' }
 }, function (err, obj) {
        if (err) return cb(err);
        cb(null, obj);
});

Here is the code that I would expect to work for the Account:

Account.find({
        where: {
            id: id,
        },
        include: {
            relation: 'branches',
            scope: {
                include: [
                    {
                        relation: 'incidents',
                        scope: {
                            where: {
                                incidentDate: {
                                    between: [
                                        beginDate,
                                        today
                                    ]
                                }
                            }
                        }
                    },
                    { relation: 'riskAssessments' },
                    { relation: 'claims' }
                ]
            }
        }
    }, function (err, obj) {
            if (err) return cb(err);
            cb(null, obj);
    });

However, this only gives me the Account and an empty array of Branches. If I run it with this code:

Account.find({
        where: {
            id: id,
        },

        include: {
            relation: 'branches',
        }
 },function (err, obj) {
            if (err) return cb(err);
            cb(null, obj);
 });

It gives me the Account with the related Branches. Whenever I add the

scope: {
    include: {
        relations: 'riskAssessments',
    }
}

it doesn't work, and returns the empty Branch array. I can add other parameters in the scope like fields, where, etc and they work as expected, it's just when I add the include.


Solution

  • The issue I was running into is related to a bug that can be found here: https://github.com/strongloop/loopback/issues/2356

    In a nutshell, ensure none of your IDs are 0 or you will run into this issue.