我有 JSON 列,其中存储了类似 - 的数据
{ tag : ["as","bs","cs"] }
我想使用 ILIKE
在此列中搜索,并且我相信 JSON 数据类型只是字符串,所以我使用了类似的查询 -
SELECT * FROM public."Transactions" WHERE tags::text ILIKE '%as%'
上面的查询在 sql 中运行良好
我需要用sequelize模型来实现这个但没有成功 我使用的代码是
let searchQuery = [
{
payee: {
[Op.iLike]: '%' + search + '%'
}
},
{
tags: {
[Op.iLike]: '%as%'
}
}
];
给出错误
Unhandled rejection SequelizeDatabaseError: operator does not exist: json ~~* unknown
最佳答案
https://www.postgresql.org/docs/current/static/functions-json.html
检查运算符列表 - 没有 ~~
- ILIKE
不适用于 json。您必须将 json 转换为文本:
t=# select '{ "tag" : ["as","bs","cs"] }'::json::text ilike '%as%';
?column?
----------
t
(1 row)
或使用 native 运算符:
t=# select ('{ "tag" : ["as","bs","cs"] }'::json)->'tag'->>0 = 'as';
?column?
----------
t
(1 row)
或者如果您是 9.5 及以上版本 - 转换为 jsonb 并使用其强大的运算符:
t=# select '{ "tag" : ["as","bs","cs"] }'::json::jsonb @> '{"tag":["as"]}'::jsonb;
?column?
----------
t
(1 row)
https://www.postgresql.org/docs/current/static/functions-json.html
关于json - 在sequelize where子句中使用ILIKE(JSON列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48536774/