Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have an Array of hstore in PostgreSQL

I am a complete beginner in PostgreSQL. And I was really amazed by the hstore datatype provided by Postgres. Well, I am using the Rails 3 framework and developing a simple app that uses PostgreSQL. I want to store an array of hashes in a field. For Eg.:

authors: [
           {
             name: "abc",
             email: "[email protected]"
           },
           {
             name: "xyz",
             email: "[email protected]"
           }
         ]

Is this possible in PostgreSQL using Rails 3? If so, can somebody give insights on how?

Thanks

like image 608
swaroopsm Avatar asked Apr 24 '13 14:04

swaroopsm


People also ask

Is Hstore deprecated?

hstore is deprecated. Use jsonb . @danger89 Actually, it's not formally deprecated, though I don't think there's any reason to use it in favour of jsonb anymore.

How do I use Hstore in PostgreSQL?

We enabled the PostgreSQL hstore using the Create Extension. With the help of the -> operator, we can select the data for a specific value. We have used the hstore data type within the WHERE. We can also retrieve the values from the hstore column using the avals() function.

What is Hstore?

hstore is a PostgreSQL extension that implements the hstore data type. It's a key-value data type for PostgreSQL that's been around since before JSON and JSONB data types were added.


1 Answers

It's certainly possible to create an array-of-hstore column in Rails 4 with e.g. a column spec like this in the table creation:

t.hstore :properties, :array => true

However, there's an encoding bug in Rails 4.0 that unfortunately renders them unusable; essentially you can read from them and they present correctly as arrays of hashes, but not write.

I've opened an issue (with fix patch) at https://github.com/rails/rails/issues/11135 which hopefully will be incorporated soon.

like image 179
inopinatus Avatar answered Sep 20 '22 03:09

inopinatus