2, "cc" => 3, "d" => 4 } #=> { "aaa" => 1-6ren">
gpt4 book ai didi

ruby-on-rails - 在 Hash 中保存错误排序后的 activerecord-postgres-hstore

转载 作者:行者123 更新时间:2023-11-29 12:00:39 24 4
gpt4 key购买 nike

用Hstore存储为Hash Table,保存后Hash顺序错误

class Service < ActiveRecord::Base
serialize :properties, ActiveRecord::Coders::Hstore
end

service = Service.new
service.properties = { "aaa" => 1, "zz" => 2, "cc" => 3, "d" => 4 }
#=> { "aaa" => 1, "zz" => 2, "cc" => 3, "d" => 4 }
service.save
reload!
service = Service.find(:id)
service.properties
#=> { "d" => "4", "cc" => "3", "zz" => 2, "aaa" => 1 }
Bug::: wrong ordering after save

是不是因为序列化之后是按Tree排序的。任何想法或任何人以前都遇到过这个问题?提前致谢。

最佳答案

来自fine PostgreSQL manual :

F.16. hstore
[...]
This module implements the hstore data type for storing sets of key/value pairs within a single PostgreSQL value.
[...]
The order of the pairs is not significant (and may not be reproduced on output).

因此 PostgreSQL 的 hstore 类型是一组无序的键/值对,不保证键/值对的任何特定顺序。一旦您的 Ruby 哈希转换为 hstore,排序就会丢失。

如果您需要维护哈希中的顺序,则必须使用不同的序列化格式。

关于ruby-on-rails - 在 Hash 中保存错误排序后的 activerecord-postgres-hstore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14888580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com