在where子句中将强制转换列序列化为整数

2022-01-19 00:00:00 javascript sequelize.js

试图在 where 子句中转换一列以搜索查询.很难找到有关如何执行此操作的可靠文档.有什么想法吗?

Trying to cast a column inside a where clause to search for a query against. Have a difficult time finding reliable documentation on how to do this. Any ideas?

return await User.findAndCountAll({
      where: {
        [Op.or]: {
          'email': { [Op.iLike]: `%${query}%` },
          '$id::text$': { [Op.iLike]: `%${query}%` } // id is int but needs to be string for searching
        }
      },
      limit,
      offset: page * limit,
      order: [[sortBy, sortOrder.toUpperCase()]],
      raw: true,
      logging: console.log
    });

推荐答案

我想这就是你要找的:

where: {
          [Op.or]: [
            {email: {[Op.iLike]: `%${query}%`}},
            sequelize.where(
              sequelize.cast(sequelize.col('User.id'), 'varchar'),
              {[Op.iLike]: `%${query}%`}
            ),
          ],
        },

相关文章