title: Sequelize where with include
body: <p>I am using sequelize as my backend ORM.
  But I have a problem when I want "where" with a join table. The associations are good but I didn't know how to do for the "where".</p>
  
  <p>This my code :</p>
  
  <pre><code>router.get('/id_presta_struct_unit/:id_presta_struct_unit', (req, res) =&gt; {
    models.structures.findAll({
      include: {
        required: false,
        model: models.structures_proposer_prestations,
        where: {
          id_presta_struct_unit: req.params.id_presta_struct_unit
        },
        include: {
          model : models.unites_facturation,
        }
      }
    }).then(data =&gt; {
      res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'});
      res.end(JSON.stringify(data));
    });
  });
  </code></pre>
  
  <p>I got this request</p>
  
  <p>SELECT * FROM <code>structures</code> AS <code>structures</code> LEFT OUTER JOIN <code>structures_proposer_prestations</code> AS <code>structures_proposer_prestations</code> ON <code>structures</code>.<code>id_structure</code> = <code>structures_proposer_prestations</code>.<code>id_structure</code> <strong>AND <code>structures_proposer_prestations</code>.<code>id_presta_struct_unit</code> = '1'</strong> LEFT OUTER JOIN <code>unites_facturation</code> AS <code>structures_proposer_prestations.unites_facturation</code> ON <code>structures_proposer_prestations</code>.<code>id_unite</code> = <code>structures_proposer_prestations.unites_facturation</code>.<code>id_unite</code>;</p>
  
  <p>But i would like to get </p>
  
  <p>SELECT * FROM <code>structures</code> AS <code>structures</code> LEFT OUTER JOIN <code>structures_proposer_prestations</code> AS <code>structures_proposer_prestations</code> ON <code>structures</code>.<code>id_structure</code> = <code>structures_proposer_prestations</code>.<code>id_structure</code> LEFT OUTER JOIN <code>unites_facturation</code> AS <code>structures_proposer_prestations.unites_facturation</code> ON <code>structures_proposer_prestations</code>.<code>id_unite</code> = <code>structures_proposer_prestations.unites_facturation</code>.<code>id_unite</code> <strong>WHERE <code>structures_proposer_prestations</code>.<code>id_presta_struct_unit</code> = '1'</strong>;</p>
  
  <p>I don't know what to do I didn't find a post with the same problem</p>
  
  <p>Can anyone point me in the right direction?</p>
  
  <p>Thank you in advance.</p>
  
  <p>Edit:</p>
  
  <p>The associations</p>
  
  <pre><code>models.structures_employer_ressources.hasMany(models.ressources, { foreignKey: 'id_ressource' });
  models.ressources.belongsTo(models.structures_employer_ressources, { foreignKey: 'id_ressource' });
  </code></pre>
  
  <p>The model of ressources</p>
  
  <pre><code>module.exports = function(sequelize, DataTypes) {
    return sequelize.define('ressources', {
      id_ressource: {
        type: DataTypes.STRING,
        allowNull: false,
        primaryKey: true
      }
   ........
    },{
      tableName: 'ressources',
      updatedAt: 'date_modification',
      createdAt: 'date_creation'
    });
  };
  </code></pre>
  
  <p>And the model of structures_employer_ressources</p>
  
  <pre><code>module.exports = function(sequelize, DataTypes) {
    return sequelize.define('structures_employer_ressources', {
      id_structure: {
        type: DataTypes.STRING,
        allowNull: false,
        primaryKey: true,
        references: {
          model :'structures',
          key: 'id_structure'
        }
      },
      id_ressource: {
        type: DataTypes.STRING,
        allowNull: false,
        primaryKey: true,
        references: {
          model :'ressources',
          key: 'id_ressource'
        }
      }
    },{
      tableName: 'structures_employer_ressources',
      updatedAt: 'date_modification',
      createdAt: 'date_creation'
    });
  };
  </code></pre>