0

If I added this hash values {"1"=>"1", "3"=>"3", "2"=>"2"} into hstore column into PostgreSql (9.4) (through rails 5}, this hash will be reorder under hstore column and looks like this : {"1"=>"1", "2"=>"2", "3"=>"3"}. Is it possible to prevent this?

1 Answer 1

1

Hash order is guaranteed in Ruby. I assume that because it's leaving Ruby and going to the database, you are not getting the guarantee anymore. I don't think postgres preserves the order source:

The order of the [hstore] pairs is not significant (and may not be reproduced on output)

If you really want the order to be preserved I think you're out of luck. If you want the order to be the same then you should sort the Ruby hash in a known way and then sort the SQL in the same way. I believe there's a way to sort an hstore column by keys: Order by a value of an arbitrary attribute in hstore

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.