title: Problem using both include and attributes in findAll() query in Sequelize
body: <p>I searched on the internet a common issue but I did not find anything so I post the question here.</p>
  
  <p>I am using Sequelize for Node and I am trying to query my PostgreSQL database to find all the objects in a specific table. However, I would like to be able to replace de foreign keys by data from their linked table (In other words, join tables and remove the ids). I managed to do this by using both the "include" option to join the table and the "attributes" option to unselect the Id and only keep the joined value, but since I have a hasMany relationship, I get an error when executing the query.</p>
  
  <p>Here is the options I use : </p>
  
  <pre><code>options = {
          include: [
              {
                  model: models.ProviderType,
                  attributes: ['id', 'name'],
                  as: 'providerType',
              },
              {
                  model: models.SavingProduct,
                  attributes: ['id', 'name'],
                  as: 'savingProducts',
              },
          ],
          attributes: [
              'id',
              'name',
              'siteUrl',
              'logoPath',
              'createdAt',
              'updatedAt',
              'deletedAt',
          ],
      }
  </code></pre>
  
  <p>And it executed the following query : </p>
  
  <pre><code>SELECT "Provider".*, "providerType"."id" AS "providerType.id", "providerType"."name" AS "providerType.name", "savingProducts"."id" AS "savingProducts.id", "savingProducts"."name" AS "savingProducts.name"
  FROM (
  SELECT "Provider"."id", "Provider"."name", "Provider"."siteUrl", "Provider"."logoPath", "Provider"."createdAt", "Provider"."updatedAt", "Provider"."deletedAt" 
  FROM "Provider" AS "Provider" 
  WHERE "Provider"."deletedAt" IS NULL 
  LIMIT 20 OFFSET 0
  ) AS "Provider" 
  LEFT OUTER JOIN "ProviderType" AS "providerType" ON "Provider"."providerTypeId" = "providerType"."id"
  LEFT OUTER JOIN "SavingProduct" AS "savingProducts" ON "Provider"."id" = "savingProducts"."providerId" AND ("savingProducts"."deletedAt" IS NULL);
  </code></pre>
  
  <p>The error thrown is : "column Provider.providerTypeId does not exist". It is logical because, when selecting the embedded SELECT, I do not select Provider.providerTypeId as I don't want this column to be returned, and because of that, when trying to JOIN the tables, SQL do not find this column and return an error.</p>
  
  <p>In other words, Sequelize first select the wanted values and then join while I would like it to do the opposite, ie join the tables and then select only the asked columns.</p>
  
  <p>So I understand the error but I don't find any way to replace the foreign keys by data from the linked tables. It is possible I did not take the right direction to do that so if you see something easier and cleaner, I would really appreciate!</p>
  
  <p>Followed are the models definition : </p>
  
  <p>For Provider</p>
  
  <pre><code>
  module.exports = (sequelize, DataTypes) =&gt; {
      const Provider = sequelize.define('Provider', {
          id: {
              type: DataTypes.INTEGER,
              allowNull: false,
              primaryKey: true,
              autoIncrement: true,
          },
          name: {
              type: DataTypes.STRING,
              allowNull: false,
          },
          siteUrl: {
              type: DataTypes.STRING,
              allowNull: true,
          },
          logoPath: {
              type: DataTypes.STRING,
              allowNull: true,
          },
      }, {
          freezeTableName: true,
          paranoid: true,
      });
      Provider.associate = (models) =&gt; {
          Provider.belongsTo(models.ProviderType, { as: 'providerType' });
          Provider.hasMany(models.SavingProduct, { as: 'savingProducts', foreignKey: 'providerId' });
      };
      return Provider;
  };
  
  </code></pre>
  
  <p>For SavingProduct</p>
  
  <pre><code>
  module.exports = (sequelize, DataTypes) =&gt; {
      const SavingProduct = sequelize.define('SavingProduct', {
          id: {
              type: DataTypes.INTEGER,
              allowNull: false,
              primaryKey: true,
              autoIncrement: true,
          },
          name: {
              type: DataTypes.STRING,
              allowNull: false,
          },
      }, {
          freezeTableName: true,
          paranoid: true,
      });
      SavingProduct.associate = (models) =&gt; {
          SavingProduct.belongsTo(models.Provider, { as: 'provider', foreignKey: 'providerId' });
      };
      return SavingProduct;
  };
  
  </code></pre>
  
  <p>Please do not hesitate to tell me if I am not clear, I would be happy to provide you with more informations.</p>
  
  <p>Thank you very much!</p>