title: Sequelize - subquery in where clause
body: <p>I'm using <a href="http://docs.sequelizejs.com/en/latest/" rel="noreferrer">Sequelize</a> in my Express app. I need to generate a query that has a subquery in the <code>WHERE</code> clause.</p>
  
  <p></p>
  
  <pre><code>SELECT *
    FROM MyTable
   WHERE id NOT IN (
         SELECT fkey
           FROM MyOtherTable
          WHERE field1 = 1
            AND field2 = 2
            AND field3 = 3
         )
  </code></pre>
  
  <p>I first tried relations/associations through my models but couldn't get it to work. Something like:</p>
  
  <pre class="lang-js prettyprint-override"><code>MyTable.find( {
      where: {
          id: {
              $notIn: // &lt;= what goes here? Can I somehow reference my include model?
          }
      },
      include: [ {
          model: MyOtherTable,
          where: {
            field1: 1,
            field2: 2,
            field3: 3
      } ]
  } );
  </code></pre>
  
  <p>Then I tried using <code>Sequelize.where()</code>, no luck there.</p>
  
  <p>Then I tried <code>Sequelize.literal()</code> and that works but not sure if it's a <em>"proper"</em> way of doing a subquery in a where clause in Sequelize as I'm new to it.</p>
  
  <pre class="lang-js prettyprint-override"><code>MyTable.find( {
      where: {
          id: {
              $notIn: sequelize.literal( 
                  '( SELECT fkey ' +
                      'FROM MyOtherTable ' +
                     'WHERE field1 = ' + field1 +
                      ' AND field2 = ' + field2 +
                      ' AND field3 = ' + field3 + 
                  ')'
          }
      } 
  } );
  </code></pre>
  
  <p>I also know that I could use <code>Sequelize.query()</code> but don't really know if I should reach for it or if <code>literal()</code>is the right away as I feel like there's something I'm overlooking.</p>
  
  <p>I would really like to know how to perform a subquery in a <code>WHERE</code> clause with Sequelize the <em>"proper"</em> way.</p>
  
  <p>Thanks for the feedback!</p>