加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如何在PostgreSQL中对jsonb列类型使用“LIKE”查询?

发布时间:2020-12-17 03:50:08 所属栏目:百科 来源:网络整理
导读:对于PostgreSQL数据库中的hstore列,我知道我可以在 Ruby on Rails中使用类似的“LIKE”查询来搜索包含某个字符串的名称: Product.where("hstore_data - 'author' LIKE '%billy%'") 我尝试了jsonb列类型,但得到了这个错误: ActiveRecord::StatementInvalid:
对于PostgreSQL数据库中的hstore列,我知道我可以在 Ruby on Rails中使用类似的“LIKE”查询来搜索包含某个字符串的名称:

Product.where("hstore_data -> 'author' LIKE '%billy%'")

我尝试了jsonb列类型,但得到了这个错误:

ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:
operator does not exist: jsonb ~~ unknown
HINT: No operator matches the given name and argument type(s). You
might need to add explicit type casts. : SELECT “products”.* FROM
“products” WHERE (jsonb_data -> ‘author’ LIKE ‘%billy%’)

有没有办法正确使用“LIKE”的jsonb列类型?

解决方法

你可以试试这个

希望你有

product.jsonb_data = {
      author: "billynando"
   }

然后

Product.where("jsonb_data ->> :key LIKE :value",key: "author",value: "%billy%"
)

更多here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读