Sequelize: Or-condition over multiple tables

自闭症网瘾萝莉.ら 提交于 2021-02-04 16:06:59

问题


I want to add an or condition over multiple tables with sequelizejs. My problem is that I don't know how to use the or operators ($or and Sequelize.or) over more than one table.

Let's say I want to implement the following sql-query:

select * from A as a, B as b, C as c where (A.b_id = b.id and b.x = 7) or (A.c_id = C.id and c.z = "test")

I would implement only the first condition with sequelize like this:

A.findAll({
   include: [{ model: B, where: { x: 7 } }]
})

And only the second condition like this:

A.findAll({
   include: [{ model: C, where: { z: "test" } }]
})

But how do I combine both queries into the one I want?


回答1:


you can do something like this

A.findAll({
   include: [{model: B},{model: C}], 
   where: {
     '$or':{
        '$b.x$': 7, 
        '$c.z$': "test"
     }
   });


来源:https://stackoverflow.com/questions/39062925/sequelize-or-condition-over-multiple-tables

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