MI Sabic
MI Sabic

Reputation: 387

Nested inner join in Sequelize

I am trying to Inner Join five tables using Sequelize. I know I have to use required: true for inner join. But even after using the required: true it is not generating the query I am trying to achieve. Here, I have attached the code without the required: true statement. How should I place the required: true statement to inner join the five tables?

const db = require('../models');

const data = await db.A.findAll({
    where: conditionA,
    include: [{
        model: db.B,
        where: conditionB,
        include: [
            {
                model: db.C,
                where: conditionC,
                include: [{
                    model: db.D,
                    where: conditionD
                }],
            },
            {
                model: db.E,
                where: conditionE,
            }
        ]
    }]
});

Model associations

db.A.hasMany(db.B);
db.C.hasMany(db.B);
db.E.hasMany(db.B);
db.D.hasMany(db.C);

db.B.belongsTo(db.A);
db.B.belongsTo(db.C);
db.B.belongsTo(db.E);
db.C.belongsTo(db.D);

Any help would be appreciated. Thanks in advance.

Upvotes: 5

Views: 4550

Answers (1)

Anatoly
Anatoly

Reputation: 22803

You should indicate required: true for all models in include options and indicate subQuery: false to make Sequelize to use usual JOINs instead of JOINs with subqueries.

Upvotes: 3

Related Questions