[我有一个带有hstores数组的Postgres表,hstore[]
,我正在Ruby中使用Sequel gem来处理它。
虽然不起作用。它返回hstore数组的原始字符串表示形式,所以当我调用DB[:invoices][id: 1337].line_items
时,我得到:
"{\"\\\"amount\\\"=>\\\"795\\\", \\\"description\\\"=>\\\"Box\\\"\",\"\\\"amount\\\"=>\\\"200\\\", \\\"description\\\"=>\\\"Shipping\\\"\"}"
代替
[{'amount' => 795, 'description' => 'Box' ...}]
我像这样连接到数据库:
DB = Sequel.connect(ENV.fetch('DATABASE_URL')).tap do |db|
Sequel.extension :pg_hstore_ops
db.extension :pg_hstore
db.extension :pg_array
db.extension :pg_json
db.extension :pg_streaming
end
似乎Sequel扩展名与订单有关。仅当将db.extension :pg_hstore
称为after db.extension :pg_array
时,此方法才有效。
所以数据库的配置块应该是:
DB = Sequel.connect(ENV.fetch('DATABASE_URL')).tap do |db|
Sequel.extension :pg_hstore_ops
db.extension :pg_array
db.extension :pg_hstore # This is now after :pg_array!
db.extension :pg_json
db.extension :pg_streaming
end