nested include is not showing data for second include

Viewed 172

I am trying to get 2 left outer join from sequelize by using nested include. But, second include inside 1st include is not returning table's data.

call.hasMany(session,{
    as: "sessions"
})
session.belongsTo(call,{
    foreignKey: "call_id",
    as: "call"  
})
session.hasMany(session_extra_data,{
    as: "sessionExtraData"
})
session_extra_data.belongsTo(session,{
    foreignKey: "session_id",
    as: "session"
})
call.findAll({
    limit: 5,   
    include: [{
        model: session,
        as: "sessions",
        indlude:[{
            model: session_extra_data,
            as: "sessionExtraData",
        }],
        required: false
    }],
}).then(data => {
    console.log("DATA", JSON.stringify(data));
})

call model

const call = sequelize.define('call', {
    id: {
        type: Sequelize.BIGINT,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true, 
        field: 'id'
    },
    version: {
        allowNull: false,
        type: Sequelize.BIGINT,
        field: 'version'
    },
    
    
},{
    freezeTableName:true,
    timestamps: false
});

Session model

const session = sequelize.define('session', {
    id: {
        type: Sequelize.BIGINT,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true, 
        field: 'id'
    },
    version: {
        field: 'version',
        type: Sequelize.BIGINT,
        allowNull: false,
    },
    callId : {
        field: 'call_id',
        type: Sequelize.BIGINT,
        allowNull: false
    },
},{
    freezeTableName:true,
    timestamps: false
});

session extra model

const session_extra_data = sequelize.define('session_extra_data', {
    id: {
        type: Sequelize.BIGINT,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true, 
        field: 'id'
    },
    version: {
        field: 'version',
        type: Sequelize.BIGINT,
        allowNull: false,
    },
    tenantId: {
        field: 'tenant_id',
        type: Sequelize.STRING(255),
        allowNull: false,
    },
    sessionId : {
        field: 'session_id',
        type: Sequelize.BIGINT,
        allowNull: false
    },
},{
    freezeTableName:true,
    timestamps: false
});

I have put all the model definition in code. you can see and tell me what i am doing wrong.

there is foreign key in session table data of call id as call_id there is foreign key in session-extra-data table data of Session id as session_id

1 Answers

This is probably happening because there are some missing properties being defined on the associations. As a result, there are probably multiple columns in the database that are being used as foreign keys. Try this instead:

call.hasMany(session,{
    as: "sessions",
    foreignKey: "call_id",
    sourceKey: "id"
})
session.belongsTo(call,{
    foreignKey: "call_id",
    targetKey: "id",
    as: "call"  
})
session.hasMany(session_extra_data,{
    as: "sessionExtraData",
    foreignKey: "session_id",
    sourceKey: "id"
})
session_extra_data.belongsTo(session,{
    foreignKey: "session_id",
    targetKey: "id",
    as: "session"
})
call.findAll({
    limit: 5,   
    include: [{
        model: session,
        as: "sessions",
        indlude:[{
            model: session_extra_data,
            as: "sessionExtraData",
        }],
        required: false
    }],
}).then(data => {
    console.log("DATA", JSON.stringify(data));
})

You might also want to check your database schema to make sure that there aren't any extra foreign key columns in the sessions and session_extra_data tables.... This might happen if

await sequelize.sync()

was being executed at some point in the code.

Edit On the model definitions for sequelize, try the following for the foreign keys:

const session = sequelize.define('session', {
    // ...
    callId : {
        field: 'call_id',
        type: Sequelize.BIGINT,
        allowNull: false,
        references: {
            model: call,
            key: 'id'
        }
    },
    // ...
}, {
    freezeTableName:true,
    timestamps: false
})

// ...

const session_extra_data = sequelize.define('session_extra_data', {
    // ...
    sessionId : {
        field: 'session_id',
        type: Sequelize.BIGINT,
        allowNull: false,
        references: {
            model: session,
            key: 'id'
        }
    },
    // ...
}, {
    freezeTableName:true,
    timestamps: false
})
Related