title: Including data via several linking tables in Sequelize
body: <p>I have the following tables in MySQL database:</p>
  <ol>
  <li><p>Bands { id, name, ... }</p>
  </li>
  <li><p>People { id, name, ... }</p>
  </li>
  </ol>
  <p>// A person can be in one or more bands
  // A band has one or more members</p>
  <ol start="3">
  <li><p>BandMembers { id, personId, bandId, isGuest, ... }</p>
  </li>
  <li><p>Instruments { id, name, ... }</p>
  </li>
  </ol>
  <p>// A band member can play one or more instruments in the same specific band
  // Several band members of the same band can play same instruments (e.g. two guitarists, all members sing, etc.)
  // A person can play different instruments in different bands he or she is in</p>
  <ol start="5">
  <li>BandMembersRoles { id, memberId, instrumentId, ... }</li>
  </ol>
  <p>Now, if I want to get all members from a specific band, I would write a query like this:</p>
  <pre><code>SELECT
      p.id AS personId,
      p.name,
      bm.isGuest,
      bm.id AS bandMemberId
  
  FROM people AS p
  JOIN BandMembers AS bm
      ON p.id = bm.Id
  WHERE bm.bandId = 1
  </code></pre>
  <p>And in Sequelize the same result would be achieved by this:</p>
  <pre><code>// In models config:
  
  db.People.belongsToMany(db.Bands, {
      through: 'BandMembers',
      foreignKey: &quot;personId&quot;
  })
  
  db.Bands.belongsToMany(db.People, {
      through: 'BandMembers',
      as: 'members',
      foreignKey: &quot;bandId&quot;
  })
  
  . . .
  
  
  // In models config:
  
  exports.findByBand = (req, res) =&gt; {
      Person.findAll({
      // This is a way to get band members only by specific band, please let me know if there is more elegant way for this:
          include: [{
              model: Band,
              attributes: [],
              where: { id: req.params.id }
          }],
          attributes: [
              ['id', 'personId'],
              [db.Sequelize.col('bands.BandMembers.id'), 'bandMemberId'],
              ['name'],
              [db.Sequelize.col('bands.BandMembers.isGuest'), 'isGuest']
          ]
      })
      .then(data =&gt; {
          // ...
      })
  }
  </code></pre>
  <p>Now, if wanted each band member's role in the specific band, in PHP I would create a code like this:</p>
  <pre><code>// First, store the MySQL Query above in $personQuery
  
  $memStmt = $conn-&gt;prepare($personQuery);
  $memStmt-&gt;bindParam(':bandId', $bandId);
  $memStmt-&gt;execute();
  
  $memArr = array();
  
  while ($mem = $memStmt-&gt;fetch(PDO::FETCH_ASSOC)) {
      extract($mem);
  
      // *** Get Roles
      $roleArr = array();
  
      $roleQuery = &quot;SELECT
              r.id AS id,
              r.name
          FROM roles AS r
              JOIN BandMembersRoles AS bmr
                  ON bm.id = r.memberId
              WHERE bm.id = :bandMemberId;
  
      $roleStmt = $conn-&gt;prepare($roleQuery);
      $roleStmt-&gt;bindParam(':m2b_id', $m2b_id);
      $roleStmt-&gt;execute();
  
      while ($role = $roleStmt-&gt;fetch(PDO::FETCH_ASSOC)) {
          extract($role);
  
          $roleItem  = array(
              'id' =&gt; (int)$id,
              'name' =&gt; $name
          );
  
          array_push($roleArr, $roleItem);
      }
      // *** End of Get Roles
  
  // and the add it to member object...
  </code></pre>
  <p>. . .</p>
  <p>So, my question is: is it possible in Sequelize to include the instruments of a band member via some include code or by any other way?</p>
  <p>Thank you!</p>