postgresql - Searching hstore with ruby on rails returning nothing -
i have hstore field called extra_info key 'real_params' , value hash . in database pair "real_params"=>"{\"master\":\"13\",\"reference_month\":\"58\",\"distributor\":\"14\"}".
seeking through rails console storage.where("extra_info -> 'params' = '{\"master\":\"13\",\"reference_month\":\"58\",\"distributor\":\"14\"}'") returns nothing, changing params order storage.where("extra_info -> 'params' = '{\"master\":\"13\",\"distributor\":\"14\",\"reference_month\":\"58\"}'") find it.
why order not same? why happining?
the hstore column type storing key/value pairs both key , value strings (at to the lowest degree until postgresql 9.4). value in hstore appears json representation of hash that's still string. these two:
'{"master":"13","reference_month":"58","distributor":"14"}' '{"master":"13","distributor":"14","reference_month":"58"}' are different strings though they're equivalent hashes 1 time parse json.
you'll have improve time if can switch json column instead of hstore. utilize #> operator @ paths json care about.
if must remain hstore need utilize fixed key order in hashes (make sure they're in alphabetical order example) , hope json preserve key order. you'll have rebuild hstore values before database (probably using before_validation hook) , might mean rebuilding whole hstore column.
ruby-on-rails postgresql rails-activerecord hstore
No comments:
Post a Comment