【问题标题】:Postgres HStore Errors - Unknown OperatorPostgres HStore 错误 - 未知运算符
【发布时间】:2012-08-11 08:13:51
【问题描述】:

我的红宝石代码:

Portfolio.where("data @> (:key => :value)",     :key => 'CSJ', :value => '0.1')

生成以下 SQL:

"SELECT \"portfolios\".* FROM \"portfolios\"  WHERE (data @> ('CSJ' => '0.1'))"

出现此错误:

Error: PG::Error: ERROR:  operator does not exist: unknown => unknown
LINE 1: ...olios".* FROM "portfolios"  WHERE (data @> ('CSJ' => '0.1'))
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "portfolios".* FROM "portfolios"  WHERE (data @> ('CSJ' => '0.1'))

Postgresql 9.1.4,Rails 3.2.7/8,在我的模型代码中使用带有以下内容的 activerecord-postgres-hstore gem:

serialize :data, ActiveRecord::Coders::Hstore

我们将不胜感激!

【问题讨论】:

  • 我不知道 => 是什么……你的意思是 >=(大于或等于)吗?
  • 啊,谢谢。以前没见过。
  • 感谢您提供详细的问题,并感谢您从一开始就包含您正在使用的版本。为大家节省大量时间。

标签: sql ruby-on-rails ruby postgresql hstore


【解决方案1】:

您没有在 Rails 使用的数据库中安装 hstore 扩展。

例如,如果我在我的一个没有 hstore 的数据库中说 select 'a' => 'b',我会得到:

=> select 'a' => 'b';
ERROR:  operator does not exist: unknown => unknown
LINE 1: select 'a' => 'b';
                   ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

但是在另一个安装了 hstore 的数据库中,我得到了这个:

=> select 'a' => 'b';
 ?column? 
----------
 "a"=>"b"
(1 row)

您需要在 Rails 数据库中创建create extension hstore

【讨论】:

  • 非常感谢您的确认。我最初考虑过这一点,然后在我安装了扩展程序时决定不可能是这种情况。但是你的笔记让我想起了上次我把这些数据库带进来的时候,我很懒惰,从一个转储中重新生成了它们。那一定是问题....在检查之前我会接受,因为我现在相当确定这是问题所在!谢谢!
  • @Brandon:在你的测试套件中添加一个检查可能是有意义的,it 'should have had "create extension hstore" done in the database' do ...
  • 我在使用 Rails 3.2.13 和 psql (PostgreSQL) 9.2.4 时遇到了同样的问题。但是,我已经完成了create extension hstore。帮忙?
  • 忽略 - 语法问题。见:github.com/softa/activerecord-postgres-hstore
  • 这个问题为我解决了:stackoverflow.com/questions/14707975/…
猜你喜欢
  • 2013-01-10
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
  • 1970-01-01
  • 2021-07-10
  • 2015-01-01
  • 2020-01-17
  • 2011-06-12
相关资源
最近更新 更多