title: Error: Tried to select attributes using Sequelize.cast or Sequelize.fn without specifying an alias for the result, during eager loading
body: <p>Data: an <code>Office</code> has many <code>OfficeLocations</code>, each of them has many <code>Ratings</code>. I need to write a query, that fetches only <code>Offices</code>, that have at lest one <code>Rating</code>. My query:</p>
  
  <pre><code>let condition = {
      include: [{
          model: OfficeLocation.unscoped(),
          attributes: [
              '"Office"."id" as "Office.id"',
              '"OfficeLocations"."id" AS "OfficeLocation.id"'
          ],
          include: [
              {
                  model: Rating.unscoped(),
                  attributes: [
                      '*',
                      sequelize.fn('COUNT', sequelize.col('"OfficeLocations-&gt;Ratings"."id"'))
                  ]
              }
          ],
          group: '"Office.id", "OfficeLocation.id"',
          having: sequelize.where(
              sequelize.fn('COUNT', sequelize.col('"OfficeLocations-&gt;Ratings"."id"')),
              '&gt;',
              0
          )
      }]
  }
  
  Office.findAll(condition).then(data =&gt; {
      res.send(data);
  }).catch(e =&gt; {
      console.log(e);
  });
  </code></pre>
  
  <p>But I have an error in console:</p>
  
  <pre><code>Error: Tried to select attributes using Sequelize.cast or Sequelize.fn without specifying an alias for the result, during eager loading. This means the attribute will not be added to the returned instance
  at include.attributes.map.attr (/Users/.../node_modules/sequelize/lib/dialects/abstract/query-generator.js:1307:17)
  at Array.map (&lt;anonymous&gt;)
  at Object.generateInclude (/Users/.../node_modules/sequelize/lib/dialects/abstract/query-generator.js:1287:52)
  at Object.generateInclude (/Users/.../node_modules/sequelize/lib/dialects/abstract/query-generator.js:1355:39)
  </code></pre>
  
  <p>I've also tried </p>
  
  <pre><code>where: sequelize.literal('COUNT(DISTINCT(`OfficeLocations`.`Ratings`.`id`)) &gt; 0'),
  </code></pre>
  
  <p>instead of attributes/group/having, but it doesn't work too.</p>
  
  <p>Thanks.</p>
  
  <p><strong>Update</strong></p>
  
  <p>This SQL query works as I need:</p>
  
  <pre><code>sequelize.query(`
      SELECT
      "Office"."id" as "Office.id",
      "Office"."name",
      "Office"."website",
      "OfficeLocations"."id" AS "OfficeLocations.id",
      COUNT("OfficeLocations-&gt;Ratings"."id") as "RatingsCount"
  
      FROM "Companies" AS "Office"
      LEFT OUTER JOIN ( "OfficeLocations" AS "OfficeLocations"
          INNER JOIN "Ratings" AS "OfficeLocations-&gt;Ratings"
          ON "OfficeLocations"."id" = "OfficeLocations-&gt;Ratings"."OfficeLocationId"
      )
      ON "Office"."id" = "OfficeLocations"."OfficeId"
  
      GROUP BY "Office.id", "OfficeLocations.id"
      HAVING COUNT("OfficeLocations-&gt;Ratings"."id") &gt; 0
  `)
  </code></pre>
  
  <p>Except I want to fetch all data.</p>