Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize: Or-condition over multiple tables

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?

like image 910
biro Avatar asked Dec 19 '22 14:12

biro


1 Answers

you can do something like this

A.findAll({
   include: [{model: B},{model: C}], 
   where: {
     '$or':{
        '$b.x$': 7, 
        '$c.z$': "test"
     }
   });
like image 73
Haresh Vidja Avatar answered Dec 21 '22 03:12

Haresh Vidja