Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

mysql - How do I add conditions in sub-sub child models in sequelize which should impact my parent Model in findAndCountAll?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Okay so I figured the solution but adding a raw query with Joins in which I added my required conditions wherever I needed and fetched id's of the main model. Then I fetched the data using sequelize adding those ids in where clause. My code looks like this now.

const { limit, offset } = dbHelpers.GetPagination(
    req.query.limit,
    req.query.offset
);

let querySting = "(SELECT count(DISTINCT p.id) FROM ModelA p " +
    "JOIN ModelB cp ON cp.pId = p.id " +
    "JOIN ModelC cs ON cs.cpId = cp.id " +
    "JOIN ModelD ms ON ms.csId = cs.id " +
    "LEFT JOIN ModelE sa ON sa.msId = ms.id " +
    "LEFT JOIN ModelF pp ON pp.msId = ms.id " +
    "LEFT JOIN ModelG sta ON sta.ppId = pp.id " +
    "LEFT JOIN ModelH ol ON ol.cdId = cd.id " +
    "WHERE "   //you can add your conditions here
    + " ORDER BY p.id desc LIMIT  " + offset + ",  " + limit + ")"


const rawQuery = await models.sequelize.query(querySting,
    { type: QueryTypes.SELECT })
const Ids = rawQuery.map(result => result.id)

const results = await models.ModelA.findAndCountAll({
    where: {
        id: Ids
    },
    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,
                            separate: true,
                        },
                    ],
                },
            ],
        },
    ],
},
    {
        model: models.ModelG,
        include: [
            {
                model: models.ModelH,
                as: "ModelH",
            },
        ],
    },
                ],
            });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...