簡體   English   中英

在 where 子句中將強制轉換列序列化為整數

[英]Sequelize cast column to integer in where clause

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

由於@leogoesger 的回答對 Postgres 中的 JSONB 字段不起作用,我嘗試了這個:

    where: {
        ["data.createdAt::timestamp"]: {
            [Op.lt]: "now() - interval '3 days'"
        }
    }

瞧,它產生了:

WHERE CAST(("User"."data"#>>'{createdAt}') AS TIMESTAMP) < now() - interval '3 days'

使用 Sequelize 5.1.0。

這對我有用:

Notification.findOne({ where: {
    [Op.and]: [
        sequelize.literal("cast(notify_data as CHAR) = "+game_id),
        {user: players[i].user, notify_type: type.id}
    ]
}});

基本上, sequelize.literal返回一個字符串而不是一個鍵值對,所以我做了一個使用Op.and的工作,它可以在其數組中獲取字符串和對象

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM