如何使用续集从PostgreSQL正确返回hstores

问题描述 投票:0回答:1

我有一个带有hstore数组的PostgreSQL表,hstore[],我正在使用Sequel,但它不起作用。

它返回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
ruby postgresql sequel
1个回答
1
投票

似乎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
© www.soinside.com 2019 - 2024. All rights reserved.