2014-09-06 33 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