是否可以在PostgreSQL中拥有一个hstore数组

时间:2013-04-24 14:44:34

标签: ruby-on-rails-3 postgresql activerecord postgresql-9.1

我是PostgreSQL的完全初学者。我对Postgres提供的hstore数据类型感到非常惊讶。好吧,我正在使用Rails 3框架并开发一个使用PostgreSQL的简单应用程序。我想在一个字段中存储一个哈希数组。 例如:

authors: [
           {
             name: "abc",
             email: "abc@example.com"
           },
           {
             name: "xyz",
             email: "xyz@example.com"
           }
         ]

使用Rails 3在PostgreSQL中可以实现吗?如果是这样,有人可以提供有关如何的见解吗?

由于

2 个答案:

答案 0 :(得分:7)

当然可以在Rails 4中创建一个数组的hstore列,例如:表创建中的列规范如下:

t.hstore :properties, :array => true

然而,Rails 4.0中存在编码错误,遗憾的是它们无法使用;基本上你可以从它们读取它们并且它们正确地呈现为散列数组,但不能写入。

我已经在https://github.com/rails/rails/issues/11135打开了一个问题(有修补补丁),希望很快就会合并。

答案 1 :(得分:2)

我不确定他们是否会允许你拥有一个hstore数组,但是有一些活动的记录扩展可以添加hstore和数组类型。 e.g:

https://github.com/funny-falcon/activerecord-postgresql-arrays

https://github.com/engageis/activerecord-postgres-hstore

https://github.com/tlconnor/activerecord-postgres-array

不要错过与Rails 4相关的改进:

http://blog.remarkablelabs.com/2012/12/a-love-affair-with-postgresql-rails-4-countdown-to-2013