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.