序列化多表OR语句未知列错误

时间:2018-07-09 22:24:32

标签: mysql sql node.js sequelize.js

我正在尝试对模型进行搜索,我想让所有用户的电子邮件,名字或姓氏都类似于搜索字符串,或者在UsersSecondaryEmails表(相关模型)中包含类似这样的电子邮件字符串。

因为OR语句的各个部分位于不同的表中,所以这变得有些棘手,我只能找到其他StackOverflow答案来帮助我。

这是我的查询(简体):

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'
    }]
  }]
}

未定义req.query.s时,查询将按预期运行(无OR语句),因此我知道这与我的关联无关。

在定义了req.query.s的情况下运行此查询时,我得到了

  

“子句”中的未知列“ usersSecondaryEmails.email”

这是正在生成的SQL(格式最好):

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->usersSecondaryEmails`.`id` AS `user.usersSecondaryEmails.id`,
       `user->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->usersSecondaryEmails`
       ON `user`.`id` = `user->usersSecondaryEmails`.`user_id`
 WHERE `companiesUsers`.`company_id` = '1'
       AND `companiesUsers`.`role_id` = 20;

Sequelize中关于多表OR语句的任何建议或指向文档的链接都将非常有用(我无法在文档中找到任何高级的东西)。

1 个答案:

答案 0 :(得分:1)

我不确定如何修改companiesUsersParams的Sequelize代码,但是您的最终查询应如下所示,以获得所需的输出。这可以帮助您重写Sequelize代码。

  1. 在用户上进行左联接,而不是内部联接。
  2. 您应将最后一个OR选项usersSecondaryEmails.email LIKE '%bob%'移至UsersSecondaryEmails加入条件
  3. 在where子句中,检查条件(userid或usersSecondaryEmails中应至少存在一行用于userid)

    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 ASupdatedAt, user.id AS user.id, user.first_name AS user.firstName, user.last_name AS user.lastName, user.email AS user.email, user->usersSecondaryEmails.id AS user.usersSecondaryEmails.id, user->usersSecondaryEmails.email AS user.usersSecondaryEmails.email FROM CompaniesUsers AS companiesUsers LEFT OUTER JOIN Users AS user ---- #1 ON companiesUsers.user_id = user.id AND (user.first_name LIKE '%bob%' OR user.last_name LIKE '%bob%' OR user.email LIKE '%bob%' OR) LEFT OUTER JOIN UsersSecondaryEmails AS user->usersSecondaryEmails ON companiesUsers.user_id = user->usersSecondaryEmails.user_id AND user->usersSecondaryEmails.email LIKE '%bob%' ---- #2 WHERE companiesUsers.company_id = '1' AND companiesUsers.role_id = 20 AND (user.email is Not null OR user->usersSecondaryEmails.user_id is Not Null); ---- #3