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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With