问题
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