In sequelize, I want to add a condition in findAndCountAll call in sub-sub child models. If the condition is false, it should impact on parent models and findAndCountAll should return array length 0. Currently, just that sub-sub child model's array length gets 0 and the rest of parents are being returned.
I have added more details in the code I am sharing.
My code is like this:
const { limit, offset } = dbHelpers.GetPagination(
req.query.limit,
req.query.offset
);
const results = await models.ModelA.findAndCountAll({
where: condition,
distinct: true,
limit,
offset,
order: [["id", "DESC"]],
include: [
{
model: models.ModelB,
as: "ModelB",
include: [
{
model: models.ModelC,
separate: true,
},
{
model: models.ModelD,
separate: true,
include: [
{
model: models.ModelE,
separate: true,
{
model: models.ModelF,
where: "I want to add my condition here which should impact the whole query if not found just like the condition I have added in ModelG",
separate: true,
},
],
},
],
},
],
},
{
model: models.ModelG,
where: "I have added a condition here and if nothing is found, my parent query returns nothing which is exactly what I want.",
include: [
{
model: models.ModelH,
as: "ModelH",
},
],
},
],
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…