2014-09-06 36 views
0

就像在標題中一樣,我嘗試將哈希製作成hstore類型的列。爲Postgresql製作hstore值

我看過問題fabricator with hstore attribute,但是那裏的解決方案並不適合我。

我的hstore列名稱是「狀態」,那裏我想設置三個標誌:「已處理」,「重複」,「EOL」。我使用續集(4.14.0)作爲ORM,製造(2.8.1),紅寶石2.1.2當然PostgreSQL的)

殼體1:

status {eol: true, duplicate: false, processed: true} 

結果:

syntax error

殼體2:

status {"heol"=>"true", "hduplicate"=>"false", "hprocessed"=>"true"} 

結果:

syntax error

殼體3:

status do 
    {"heol"=>"true", "hduplicate"=>"false", "hprocessed"=>"true"} 
    end 

結果:

Sequel::DatabaseError: PG::DatatypeMismatch: ERROR: column "status" is of type hstore but expression is of type boolean LINE 1: ...23.0, '2000-01-01', (('heol' = '... HINT: You will need to rewrite or cast the expression.

殼體4:

status do 
    {status: "heol:true"} 
    end 

結果:

Failure/Error: Fabricate(:entry) Sequel::DatabaseError: PG::UndefinedColumn: ERROR: column "status" does not exist LINE 1: ...123.0, '2000-01-01', ("status" =... HINT: There is a column named "status" in table "entries", but it cannot be referenced from this part of the query.

殼體5:

status do {'status' => "heol:true"} end

結果:

Failure/Error: Fabricate(:entry) 
Sequel::DatabaseError: 
    PG::DatatypeMismatch: ERROR: column "status" is of type hstore but expression is of type boolean 
    LINE 1: ...123.0, '2000-01-01', ('status' =... 
    HINT: You will need to rewrite or cast the expression. 

殼體6: 放棄) 結果: 這個問題

隨着FactoryGirl一切正常,和語法很簡單:

FactoryGirl.define do 
    factory :entry do 
    status {{ flag_processed: true, flag_duplicate: false }} 
end 

承諾要利用好正確的語法在製作=) 謝謝!

盧卡斯。

回答

1

情況1和2絕對不是你想要的。 Hash需要在塊中指定,這與FactoryGirl使用包含雙大括號的示例相同。情況3,4和5通常可以工作,但不會,因爲Sequel具有分配hstore列的特殊語法,Fabrication不會自動爲您翻譯它(因爲在您提起之前我不知道這是一件事情)。

如果改成這樣,我想你會發現成功:

status do 
    Sequel.hstore("heol"=>"true", "hduplicate"=>"false", "hprocessed"=>"true") 
end 
+0

它的工作完美,謝謝! – CloudRide 2014-09-06 20:37:34

+0

請注意:要使用Sequel.hstore方法,您需要在數據庫實例(DB)上啓用Sequel的pg_hstore擴展: DB.extension:pg_hstore – CloudRide 2014-09-06 20:39:04