Rails:如何检查Postgres中的jsonb列是否为空

6

我在检查表中的jsonb列为空时遇到了问题。

当我的列directions为空时,它的值为"{}"

尝试了以下方法:

Model.where("directions != '{}'") <- brings all
Model.where("directions <@ '{}'") <- brings all

我是否还有其他不知道的方法?使用PostgreSQL 9.6


Model.where("directions != '{}'") 可以工作,但 Model.where.not(directions: '{}') 更符合 Rails 的风格(但等效)。如果这些查询返回了所有数据,那么您确定有任何实例包含实际的方向数据吗? - Frost
@Frost,唯一有效的语法是 Model.where.not(directions: '{}')。回答你的问题,是的,我有大小写记录。 - Petros Kyriakou
3个回答

3
Model.where.not(directions: '{}')

1
ERROR: operator does not exist: json <> unknown - phil pirozhkov
@philpirozhkov 要检查一下这4-5年间是否有什么变化 :) - Deepak Mahakale

1
Model.where.not("directions::text = ?", "{}")

-3
尝试使用.not来否定查询:
Model.where.not("directions = '{}'")

我同意Petros的观点,这在2017年和Rails 4中是无效的语法。 - user478798

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接