【问题标题】:How do I create a hstore column in a Sequel migration?如何在 Sequel 迁移中创建 hstore 列?
【发布时间】:2014-02-03 02:27:06
【问题描述】:

如何在 Sequel 迁移中创建 hstore 列?

Sequel.migration do
  change do
    add_column :logs, :geo, HStore
  end
end

失败。我必须加载扩展程序吗?

【问题讨论】:

  • 我怀疑您的答案在本文档中,sequel.jeremyevans.net/rdoc/files/doc/postgresql_rdoc.html。具体来说,查看标题PostgreSQL-specific Database Type Support
  • 感谢您为我指出正确的文档页面,但我仍然无法运行迁移。我尝试了不同的名称,例如 :hstore、:h_store、:pg_hstore、HStore 等。我已经加载了 :pg_store DB 扩展,甚至将 sequel_pg 和 sequel-hstore gem 添加到 Gemfile。尝试使用 json 数据类型并且工作正常,但我更喜欢 hstore。
  • 也许这是 gem 中的错误。你向维护者报告了吗?
  • 我会查看G.Group,谢谢。

标签: ruby postgresql migration sequel hstore


【解决方案1】:

我在文档中找不到这个,所以我在 IRC 上询问。

jeremyevans:使用method_missing,允许您使用任何自定义数据库类型

所以你可以指定jsonjsonb,只要启用扩展即可:

Sequel.migration do
  change do
    create_table :foo do
      primary_key :id
      jsonb :bar
    end
  end
end

启用扩展:

Sequel.extension :pg_json

并创建一个新记录:

foo = Foo.new bar: Sequel.pg_jsonb({ 'baz' => 'qux' })

【讨论】:

    【解决方案2】:

    作为Author's gem answered me,数据库在使用前需要这个额外的扩展:

    CREATE EXTENSION hstore
    

    【讨论】:

    • 顺便说一句,正确的行是 add_column :logs, :geo, :hstore # or 'hstore'
    猜你喜欢
    • 1970-01-01
    • 2013-07-14
    • 2012-05-20
    • 2017-01-20
    • 2016-05-15
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    相关资源
    最近更新 更多