Sequelize - Join with multiple column

有些话、适合烂在心里 提交于 2019-12-12 09:41:07

问题


I like to convert the following query into sequelize code

select * from table_a 
inner join table_b 
on table_a.column_1 = table_b.column_1
and table_a.column_2 = table_b.column_2

I have tried many approaches and followed many provided solution but I am unable to achieve the desired query from sequelize code.

The max I achieve is following :

select * from table_a 
inner join table_b 
on table_a.column_1 = table_b.column_1

I want the second condition also.

and table_a.column_2 = table_b.column_2

any proper way to achieve it?


回答1:


You need to define your own on clause of the JOIN statement

ModelA.findAll({
    include: [
        {
            model: ModelB,
            on: {
                col1: sequelize.where(sequelize.col("ModelA.col1"), "=", sequelize.col("ModelB.col1")),
                col2: sequelize.where(sequelize.col("ModelA.col2"), "=", sequelize.col("ModelB.col2"))
            },
            attributes: [] // empty array means that no column from ModelB will be returned
        }
    ]
}).then((modelAInstances) => {
    // result...
});


来源:https://stackoverflow.com/questions/42226351/sequelize-join-with-multiple-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!