【发布时间】:2021-09-16 14:52:23
【问题描述】:
在一个假设的 books 表上,其中有一个名为 internal 的 json 列,我想调用 有 和(分别)没有 有的记录rating 键。
【问题讨论】:
标签: ruby-on-rails json postgresql arel
在一个假设的 books 表上,其中有一个名为 internal 的 json 列,我想调用 有 和(分别)没有 有的记录rating 键。
【问题讨论】:
标签: ruby-on-rails json postgresql arel
存在:
Book.where('internal::jsonb ? :key', key: 'rating')
不存在:
Book.where.not('internal::jsonb ? :key', key: 'rating')
See table called "Additional jsonb Operators":
jsonb ? text → boolean
Does the text string exist as a top-level key or array element within the JSON value?
'{"a":1, "b":2}'::jsonb ? 'b' → t
'["a", "b", "c"]'::jsonb ? 'b' → t
【讨论】:
? 运算符和占位符时要小心,您需要使用命名占位符:where('internal::jsonb ? :key', key: 'rating')。