PG::UndefinedFunction: ERROR: operator does not exist: text = boolean
PG::UndefinedFunction: ERROR: operator does not exist: text = boolean
有一个具有此结构的 JSONB information
字段:
{
"ignore"=>false
}
我想获取所有 ignore
字段为 true
:
的记录
@user.posts.where("information ->> 'ignore' = TRUE")
这一行抛出错误:
PG::UndefinedFunction: ERROR: operator does not exist: text = boolean
而且我在 Google 中找不到任何内容。我们到处都在谈论文本含义。但是没有关于布尔值的内容。
您必须将 information->>'ignore'
的结果转换为布尔值:
@user.posts.where("(information ->> 'ignore')::boolean = TRUE")
我在升级到 Rails 5/6 时遇到了同样的问题。 pg
gem 转换的方式似乎有所改变,但这对我有用:
@user.posts.where("(information ->> 'ignore')::boolean = ?", true)
当一个参数作为第二个参数添加到 where
方法调用时,ActiveRecord 会为您进行适当的转换。如果将 .explain
添加到上面的查询中,您应该会看到如下内容:
EXPLAIN for: SELECT ... AND ((information ->> 'ignore')::boolean = TRUE) ...
有一个具有此结构的 JSONB information
字段:
{
"ignore"=>false
}
我想获取所有 ignore
字段为 true
:
@user.posts.where("information ->> 'ignore' = TRUE")
这一行抛出错误:
PG::UndefinedFunction: ERROR: operator does not exist: text = boolean
而且我在 Google 中找不到任何内容。我们到处都在谈论文本含义。但是没有关于布尔值的内容。
您必须将 information->>'ignore'
的结果转换为布尔值:
@user.posts.where("(information ->> 'ignore')::boolean = TRUE")
我在升级到 Rails 5/6 时遇到了同样的问题。 pg
gem 转换的方式似乎有所改变,但这对我有用:
@user.posts.where("(information ->> 'ignore')::boolean = ?", true)
当一个参数作为第二个参数添加到 where
方法调用时,ActiveRecord 会为您进行适当的转换。如果将 .explain
添加到上面的查询中,您应该会看到如下内容:
EXPLAIN for: SELECT ... AND ((information ->> 'ignore')::boolean = TRUE) ...