[英]Sequelize cast column to integer in where clause
Trying to cast a column inside a where clause to search for a query against.尝试在 where 子句中强制转换列以搜索查询。 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
});
I think this is what you searching for:我认为这就是您要搜索的内容:
where: {
[Op.or]: [
{email: {[Op.iLike]: `%${query}%`}},
sequelize.where(
sequelize.cast(sequelize.col('User.id'), 'varchar'),
{[Op.iLike]: `%${query}%`}
),
],
},
As @leogoesger's answer didn't work for JSONB fields in Postgres, I tried this:由于@leogoesger 的回答对 Postgres 中的 JSONB 字段不起作用,我尝试了这个:
where: {
["data.createdAt::timestamp"]: {
[Op.lt]: "now() - interval '3 days'"
}
}
Lo and behold, it produced:瞧,它产生了:
WHERE CAST(("User"."data"#>>'{createdAt}') AS TIMESTAMP) < now() - interval '3 days'
Using Sequelize 5.1.0.使用 Sequelize 5.1.0。
This worked for me:这对我有用:
Notification.findOne({ where: {
[Op.and]: [
sequelize.literal("cast(notify_data as CHAR) = "+game_id),
{user: players[i].user, notify_type: type.id}
]
}});
Basically, sequelize.literal returns a string rather than a key-value pair, so I did a work around using Op.and which can take both strings and objects in its array基本上, sequelize.literal返回一个字符串而不是一个键值对,所以我做了一个使用Op.and的工作,它可以在其数组中获取字符串和对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.