{"id":8046,"date":"2015-11-22T03:00:42","date_gmt":"2015-11-22T03:00:42","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/22\/join-across-multiple-junction-tables-with-sequelize-open-source-projects-sequelize-sequelize\/"},"modified":"2022-08-30T15:03:03","modified_gmt":"2022-08-30T15:03:03","slug":"join-across-multiple-junction-tables-with-sequelize-open-source-projects-sequelize-sequelize","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/22\/join-across-multiple-junction-tables-with-sequelize-open-source-projects-sequelize-sequelize\/","title":{"rendered":"Join across multiple junction tables with Sequelize-open source projects sequelize\/sequelize"},"content":{"rendered":"<p>I have a database with three primary tables: &#8220;<em>users<\/em>&#8220;, &#8220;<em>teams<\/em>&#8220;, and &#8220;<em>folders<\/em>&#8221; joined by two junction tables, &#8220;<em>users_teams<\/em>&#8221; and &#8220;<em>teams_folders<\/em>&#8220;. There is a many-to-many relationship between users and teams and between teams and folders (a user can be on more than one team and teams can own more than one folder).<\/p>\n<p>Sequelize does a wonderful job of managing the user-teams and teams-folder relationship, but I can find no way to establish a relationship between users and folders.<\/p>\n<p><strong>Is there any way to join across two junction tables without resorting to raw SQL?<\/strong><\/p>\n<p>There seems to be no way to accomplish this elegantly or in a reasonable number of steps. I have tried methods like user.getFolders(), Folder.findAll({ include: [User] }), but Sequelize doesn&#8217;t seem to be able to understand a three level hierarchy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a database with three primary tables: &#8220;users&#8220;, &#8220;teams&#8220;, and &#8220;folders&#8221; joined by two junction tables, &#8220;users_teams&#8221; and &#8220;teams_folders&#8220;. There is a many-to-many relationship between users and teams and between teams and folders (a user can be on more than one team and teams can own more than one folder). Sequelize does a wonderful [&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-8046","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8046","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=8046"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8046\/revisions"}],"predecessor-version":[{"id":8665,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8046\/revisions\/8665"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}