title: How to join the same table 2 times with different aliases and conditions using Sequelize?
body: <p>I have 3 tables:</p>
  <p>User table:</p>
  <pre><code>---------------
  userId | name 
  ---------------
  1      | Alice
  2      | Bob
  3      | Charly
  ---------------
  </code></pre>
  <p>Project table:</p>
  <pre><code>-------------------------------------
  projectId | userId | state  | color
  -------------------------------------
  4         | 1      | active | red
  5         | 1      | closed | blue
  6         | 2      | closed | green
  7         | 3      | active | yellow
  -------------------------------------
  </code></pre>
  <p>Topic table:</p>
  <pre><code>--------------------------------
  topicId | projectId | topicName
  --------------------------------
  7       | 4         | D
  8       | 4         | e
  9       | 5         | D
  10      | 6         | D
  --------------------------------
  </code></pre>
  <p>Associations:</p>
  <pre><code>User.hasMany(model.Project)
  Project.hasMany(model.Topics)
  </code></pre>
  <p>I'd like to find all <code>User</code>s who:</p>
  <ul>
  <li>have at least one <code>project</code>, which is connected to some <code>topic</code> on condition (where <code>name='D'</code>);</li>
  <li>include all <code>projects</code> that are in <code>active</code> state;</li>
  </ul>
  <p>So, as a result, I want to have</p>
  <pre><code>--------------------------------------
  userId | userName | projectId | color
  --------------------------------------
  1      | Alice    | 4         | red
  2      | Bob      | 6         | green
  --------------------------------------
  
  </code></pre>
  <p>Something like this:</p>
  <pre><code>const users = await User.findAll({
      include: [{
          model: Project,
          include: [{
              model: Topics,
              where: { name: 'D' },
              attributes: [],
          }],
          attributes: [],
      }, {
          model: Project,
          where: { status: 'active'},
          required: false,
          attributes: ['color'],
      }]
  })
  </code></pre>
  <p>I can solve this issue if I add one more association:</p>
  <pre><code>User.hasMany(models.Project, { as: 'ProjectTopics' });
  </code></pre>
  <p>and add to the query <code>as: ProjectTopics</code> on the 4th line.
  But I don't want to have two associations to the same table with the same keys, just with different aliases.</p>
  <p>There is a line in <a href="https://sequelize.org/master/class/lib/model.js%7EModel.html#static-method-findAll" rel="noreferrer">documentation</a>:</p>
  <pre><code>options.include[].through.as  
  The alias for the join model, in case you want to give it a different name than the default one.
  </code></pre>
  <p>Could you help me to understand how it works and how I can use this for my issue?</p>