title: Sequelize multi-table OR statement unknown column error
body: <p>I'm trying to do a search on my models, I want to get all users where either the email, firstName, or lastName are like the search string, or where the UsersSecondaryEmails table (related model) includes an email like that string.</p>
  
  <p>Because the parts of the OR statement are in different tables, this is getting a little tricky, and I can only find other StackOverflow answers to help me.</p>
  
  <p>Here is my query (simplified):</p>
  
  <pre><code>const companiesUsersParams = {
    where: {
      companyId: req.params.companyId,
      roleId: role.id
    },
    include: [{
      model: models.Users,
      attributes: ['id', 'firstName', 'lastName', 'email'],
      where: req.query.s ? {
        [Op.or]: [{
          firstName: {
            [Op.like]: `%${req.query.s}%`
          }
        }, {
          lastName: {
            [Op.like]: `%${req.query.s}%`
          }
        }, {
          email: {
            [Op.like]: `%${req.query.s}%`
          }
        }, {
          '$usersSecondaryEmails.email$': {
            [Op.like]: `%${req.query.s}%`
          }
        }]
      } : null,
      include: [{
        model: models.UsersSecondaryEmails,
        attributes: ['id', 'email'],
        as: 'usersSecondaryEmails'
      }]
    }]
  }
  </code></pre>
  
  <p>When <code>req.query.s</code> is not defined, the query runs as expected (No OR statement), so I know it is not an issue with my associations.</p>
  
  <p>When I run this query WITH <code>req.query.s</code> defined, I get</p>
  
  <blockquote>
    <p>Unknown column 'usersSecondaryEmails.email' in 'on clause'</p>
  </blockquote>
  
  <p>And here is the SQL being generated (formatted as best as possible):</p>
  
  <pre><code>SELECT `companiesUsers`.`id`,
         `companiesUsers`.`company_id` AS `companyId`, 
         `companiesUsers`.`user_id` AS `userId`,
         `companiesUsers`.`role_id` AS `roleId`,
         `companiesUsers`.`created_at` AS `createdAt`, 
         `companiesUsers`.`updated_at` AS`updatedAt`,
         `user`.`id` AS `user.id`,
         `user`.`first_name` AS `user.firstName`,
         `user`.`last_name` AS `user.lastName`,
         `user`.`email` AS `user.email`,
         `user-&gt;usersSecondaryEmails`.`id` AS `user.usersSecondaryEmails.id`,
         `user-&gt;usersSecondaryEmails`.`email` AS
         `user.usersSecondaryEmails.email` 
   FROM `CompaniesUsers` AS `companiesUsers`
   INNER JOIN `Users` AS `user`
         ON `companiesUsers`.`user_id` = `user`.`id` AND 
         (`user`.`first_name` LIKE '%bob%' OR
         `user`.`last_name` LIKE '%bob%' OR
         `user`.`email` LIKE '%bob%' OR
         `usersSecondaryEmails`.`email` LIKE '%bob%')
   LEFT OUTER JOIN `UsersSecondaryEmails` AS `user-&gt;usersSecondaryEmails`
         ON `user`.`id` = `user-&gt;usersSecondaryEmails`.`user_id`
   WHERE `companiesUsers`.`company_id` = '1'
         AND `companiesUsers`.`role_id` = 20;
  </code></pre>
  
  <p>Any any advice or links to documentation on multi-table OR statements in Sequelize would be great (I couldn't find anything this advanced in the documentation).</p>