3

I have two tables, Services (parent) and Invoices (children). A service can have many invoices. So their association looks something like this:

Service.hasMany(Invoice, {
    foreignKey: 'service_uuid'
});

Now I want to get a list of all the services for a user where there are no invoices associated to it.

My initial query looks like this:

Service.findAll({
    include: [
        {
            model: Invoice
        }
    ],
    where: {
        service_provider_uuid: req.user.uuid,
        service_active: 'YES'
    },
    order: [
        ['assigned_date', 'ASC']
    ]
}).catch(err => {
    next(err);
});

This query gives me all the services irrespective of whether it has invoices or not. But I am only interested in those records where the nested invoices array is empty. Now I can easily loop through this result set and map the records in a different array, but I was wanted to know if there was a way in Sequelize to get records where the related children records are completely empty.

Thanks.

codeinprogress
  • 3,193
  • 7
  • 43
  • 69
  • I hope this would help > https://stackoverflow.com/questions/66100779/how-do-i-add-conditions-in-sub-sub-child-models-in-sequelize-which-should-impact/66328066#66328066 – kamran186 Feb 23 '21 at 06:28

0 Answers0