biro
biro

Reputation: 213

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?

Upvotes: 5

Views: 5484

Answers (1)

Haresh Vidja
Haresh Vidja

Reputation: 8496

you can do something like this

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

Upvotes: 11

Related Questions