【问题标题】:Use ILIKE in sequelize where clause(JSON column)在 sequelize where 子句中使用 ILIKE(JSON 列)
【发布时间】:2018-07-10 05:52:35
【问题描述】:

我有 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%'
            }
        }
    ];

给出错误

未处理的拒绝 SequelizeDatabaseError: 运算符不存在: json ~~* 未知

【问题讨论】:

    标签: json node.js postgresql sequelize.js


    【解决方案1】:

    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)
    

    或使用原生运算符:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-26
      • 2023-03-03
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      • 2018-01-08
      • 2019-12-23
      • 1970-01-01
      相关资源
      最近更新 更多