title: Joining same table multiple times with Sequelize
body: <p>I have the following models:</p>
  
  <pre><code>const User = Sequelize.define('user', {
      login: Sequelize.DataTypes.STRING,
      password: Sequelize.DataTypes.STRING,
      is_manager: Sequelize.DataTypes.BOOLEAN,
      notes: Sequelize.DataTypes.STRING
  });
  
  const Bike = Sequelize.define('bike', {
      model: Sequelize.DataTypes.STRING,
      photo: Sequelize.DataTypes.BLOB,
      color: Sequelize.DataTypes.STRING,
      weight: Sequelize.DataTypes.FLOAT,
      location: Sequelize.DataTypes.STRING,
      is_available: Sequelize.DataTypes.BOOLEAN
  });
  
  const Rate = Sequelize.define('rate', {
      rate: Sequelize.DataTypes.INTEGER
  });
  Rate.belongsTo(User);
  User.hasMany(Rate);
  Rate.belongsTo(Bike);
  Bike.hasMany(Rate);
  </code></pre>
  
  <p>And I'd like to select bikes with their average rates, plus rates of the current user for each bike:</p>
  
  <pre><code>    Bike.findAll({
          attributes: {include: [[Sequelize.fn('AVG', Sequelize.col('rates.rate')), 'rate_avg']],
          },
          include: [{
              model: Rate,
              attributes: []
          }, {
              model: Rate,
              attributes: ['rate'],
              include: [{
                  model: User,
                  attributes: [],
                  where: {
                      login: req.user.login
                  }
              }]
          }],
          group: Object.keys(Bike.rawAttributes).map(key =&gt; 'bike.' + key) // group by all fields of Bike model
      })
  </code></pre>
  
  <p>It constructs the following query: <code>SELECT [bike].[id], [bike].[model], [bike].[photo], [bike].[color], [bike].[weight], [bike].[location], [bike].[is_available], AVG([rates].[rate]) AS [rate_avg], [rates].[id] AS [rates.id], [rates].[rate] AS [rates.rate] FROM [bikes] AS [bike] LEFT OUTER JOIN [rates] AS [rates] ON [bike].[id] = [rates].[bikeId] LEFT OUTER JOIN ( [rates] AS [rates] INNER JOIN [users] AS [rates-&gt;user] ON [rates].[userId] = [rates-&gt;user].[id] AND [rates-&gt;user].[login] = N'user' ) ON [bike].[id] = [rates].[bikeId] GROUP BY [bike].[id], [bike].[model], [bike].[photo], [bike].[color], [bike].[weight], [bike].[location], [bike].[is_available];</code></p>
  
  <p>And fails: <code>SequelizeDatabaseError: The correlation name 'rates' is specified multiple times in a FROM clause.</code></p>
  
  <p>How do I write the query right? I need Sequelize to assign another alias to the <code>rates</code> table used in the 2nd join (and add its columns to the <code>GROUP BY</code> clause, but that's the next step).</p>