0

如标题所示,我尝试将哈希编入 hstore 类型列。

我见过带有 hstore 属性的问题制造者,但那里的解决方案对我不起作用。

我的 hstore 列名是“status”,我想设置三个标志:“processed”、“duplicate”、“eol”。我正在使用续集(4.14.0)作为 ORM、制造(2.8.1)、Ruby 2.1.2 和 Postgresql,当然;)

情况1:

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

结果:

语法错误

案例2:

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

结果:

语法错误

案例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' = '. .. 提示:您将需要重写或强制转换表达式。

案例4:

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

结果:

失败/错误:Fabricate(:entry) Sequel::DatabaseError: PG::UndefinedColumn: ERROR: column "status" does not exist LINE 1: ...123.0, '2000-01-01', ("status" =. .. 提示:表“entries”中有一个名为“status”的列,但不能从这部分查询中引用它。

案例5:

状态做 {'status' => "heol:true"} 结束

结果:

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

承诺在 Fabrication 中充分利用正确的语法 =) 谢谢!

卢卡斯。

4

1 回答 1

1

案例 1 和 2 绝对不是您想要的。Hash 需要在一个块中指定,这与 FactoryGirl 对包含双括号的示例所做的相同。案例 3、4 和 5 通常可以工作,但不能正常工作,因为 Sequel 具有分配 hstore 列的特殊语法,而且 Fabrication 不会自动为您翻译(因为在您提出之前我不知道这是一回事)。

如果你把它改成这样,我想你会发现成功:

status do
  Sequel.hstore("heol"=>"true", "hduplicate"=>"false", "hprocessed"=>"true")
end
于 2014-09-06T20:11:52.050 回答