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

标签 javascript sequelize.js

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

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}%`}
            ),
          ],
        },

关于javascript - 在 where 子句中将强制转换列序列化为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47212187/

相关文章:

sequelize.js - GraphQL + Sequelize 解析器给出空数据

javascript - 在 Sequelizejs 中使用不同的 mysql 用户

javascript - 更改 Chrome 的 JS 默认行为

javascript - 如何使用 react-route 链接刷新页面

javascript - 使用从父页面到 iframe 的跳转链接

javascript - Node + Sequelize : How to check if item exists before adding?(异步混淆)

node.js - 按关联模型排序时,Sequelize 抛出错误 "Unable to find a valid association for model x"

javascript - 在具有权重/偏差的范围之间生成随机数?

javascript - 使用 jQuery 将点击事件添加到类中

node.js - 在 Sequelize 中使用事务保存关联模型