{"id":8127,"date":"2015-11-27T03:34:45","date_gmt":"2015-11-27T03:34:45","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/27\/sequelize-where-clause-with-associations-open-source-projects-sequelize-sequelize\/"},"modified":"2022-08-30T15:03:00","modified_gmt":"2022-08-30T15:03:00","slug":"sequelize-where-clause-with-associations-open-source-projects-sequelize-sequelize","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/27\/sequelize-where-clause-with-associations-open-source-projects-sequelize-sequelize\/","title":{"rendered":"sequelize &#8211; Where clause with associations-open source projects sequelize\/sequelize"},"content":{"rendered":"<p>In sequelize I have boards and users setup, with a many to many association as follows<\/p>\n<pre><code>User.hasMany(Board, {through: BoardUsers});\nBoard.hasMany(User, {through:BoardUsers});\n<\/code><\/pre>\n<p>Is there anyway that, using a where clause I can find users that belong to one of a list of boards. For example, lets says I have 3 boards and I would like to find the first 20 users (using the default sort order) who belong to either board 1 or 3. Is there a way to do this without executing separate finds for each board, then manually combining the results.<\/p>\n<p>I would love to be able to do something like:<\/p>\n<pre><code>User.findAll({where:{board: [1,3]}});\n<\/code><\/pre>\n<p>but I can&#8217;t seem to find a way to do this.<\/p>\n<p>As far as I can tell the equivalent SQL would be something like:<\/p>\n<pre><code>SELECT * FROM `users` WHERE id IN (SELECT userID FROM boardusers WHERE boardId IN (1,3))\n<\/code><\/pre>\n<p>But I would love to be able to do it through the ORM.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In sequelize I have boards and users setup, with a many to many association as follows User.hasMany(Board, {through: BoardUsers}); Board.hasMany(User, {through:BoardUsers}); Is there anyway that, using a where clause I can find users that belong to one of a list of boards. For example, lets says I have 3 boards and I would like to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8127","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8127"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8127\/revisions"}],"predecessor-version":[{"id":8599,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8127\/revisions\/8599"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}