2012-08-14 56 views
1

我的Ruby代碼:Postgres的HStore錯誤 - 未知運營商

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 

幫助將不勝感激!

+0

我不知道是什麼=>是...你的意思是> =(大於或等於)? – Jim 2012-08-14 23:58:42

+1

@Jim:http://www.postgresql.org/docs/current/static/hstore.html – 2012-08-15 00:03:18

+0

啊,謝謝。以前沒見過。 – Jim 2012-08-15 00:06:55

回答

3

您沒有在Rails正在使用的數據庫中安裝hstore擴展。

例如,如果我說select 'a' => 'b'在我的數據庫中不具有hstore之一,我得到這個:

=> 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

+0

非常感謝您的確認。我最初考慮過這個問題,然後決定不可能這樣,因爲我已經安裝了擴展。但是你的筆記讓我想起了我上次把這些數據庫帶進來的時候,我很懶,從轉儲中重新生成它們。這一定是問題....我會在檢查之前接受,因爲我現在確信這是問題所在!謝謝! – Brandon 2012-08-15 03:17:22

+0

@Brandon:爲你的測試套件增加一個檢查可能是有意義的,它應該在數據庫'do ...'中完成「創建擴展hstore」。 – 2012-08-15 03:27:25

+0

我遇到了與Rails 3.2.13和psql(PostgreSQL)9.2.4相同的問題。但是,我已經完成了「創建擴展hstore」。幫幫我? – CDub 2013-05-22 16:47:19