Sequelize - Where is the offer with associations

In sequelize I have board and user settings, with many associations as follows

User.hasMany(Board, {through: BoardUsers});
Board.hasMany(User, {through:BoardUsers});


Anyway, using the where clause, I can find users belonging to one of the board lists. For example, lets say I have 3 boards, and I would like to find the first 20 users (using the default sort order) that belong to the same board 1 or 3. Is there a way to do this without doing separate finds for each board, then manually combining the results.

I would like to do something like:

User.findAll({where:{board: [1,3]}});


but I cannot find a way to do this.

As far as I can tell, the equivalent SQL would be something like this:

SELECT * FROM `users` WHERE id IN (SELECT userID FROM boardusers WHERE boardId IN (1,3))


But I would be happy to do it through the ORM.


source to share

2 answers

While I'm not sure if you can do this directly, you can always query Boards

and eagerly get users.

Something like this:

Board.findAll({where: {id: [1,3]}}, { include: [ User ] })




Quite a late answer, but I had this same question and this is how I got it to work in Sequelize 3.24.0 (something like this);

User.findAll({ include: [{ model: Board, where: { $in: [1,3] }}] });




All Articles