【问题标题】:Fabricating hstore values for Postgresql为 Postgresql 制造 hstore 值
【发布时间】:2014-09-06 20:03:23
【问题描述】:

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

我看到了问题fabricator with hstore attribute,但那里的解决方案对我不起作用。

我的 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

结果:

续集::数据库错误: PG::DatatypeMismatch:错误:列“状态”是 hstore 类型,但表达式是布尔类型 第 1 行:...23.0, '2000-01-01', (('heol' = '... 提示:您需要重写或强制转换表达式。

案例 4:

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

结果:

失败/错误:Fabricate(:entry) 续集::数据库错误: PG::UndefinedColumn:错误:列“状态”不存在 第 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 中的正确语法 =) 谢谢!

卢卡斯。

【问题讨论】:

    标签: postgresql rspec sequel hstore fabrication-gem


    【解决方案1】:

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

    如果你改成这个,我想你会发现成功的:

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

    【讨论】:

    • 请注意:要使用 Sequel.hstore 方法,您需要在数据库实例 (DB) 上启用 Sequel 的 pg_hstore 扩展: DB.extension :pg_hstore
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    相关资源
    最近更新 更多