是否可能,以及Postgresql的HStore类型中嵌套哈希的语法是什么?

iai*_*ain 9 postgresql postgresql-9.1 hstore

我甚至不确定Postgres的HStore数据类型是否可以包含嵌套哈希,如果可以的话,如何插入它们?

这是我到目前为止所尝试的:

-- Database: test1

-- DROP DATABASE test1;
/*
CREATE DATABASE test1
  WITH OWNER = iainuser
       ENCODING = 'UTF8'
       TABLESPACE = pg_default
       LC_COLLATE = 'en_GB.UTF-8'
       LC_CTYPE = 'en_GB.UTF-8'
       CONNECTION LIMIT = -1;
*/
/* create extension hstore; */
/*drop table my_store;*/
/*
create table my_store (
  id serial primary key not null,
  doc hstore
);

CREATE INDEX my_store_doc_idx_gist
  ON my_store
  USING gist
  (doc);
*/
/* select doc from my_store; */
/*
insert into my_store (doc) values ( '"a" => "1"' );
select doc -> 'a' as first_key from my_store; -- returns "1"
*/

/* insert into my_store (doc) values ( '"b" => "c" => "3"' ); -- doesn't work */
/* insert into my_store (doc) values ( '"b" => ("c" => "3")' ); -- doesn't work */
/* insert into my_store (doc) values ( '"b" => hstore("c" => "3")' ); -- doesn't work */
/* insert into my_store (doc) values ( '"b"' => hstore("c" => "3")' ); -- doesn't work */
/* insert into my_store (doc) values ( "b"=>'"c"=>"3"'::hstore ); -- doesn't work */
Run Code Online (Sandbox Code Playgroud)

如果不可能,是否有当前接受的标准/习惯用于嵌套哈希 - 可能将它们拉开并使用id引用它们?

任何有关这方面的帮助将非常感激.

mu *_*ort 14

精细手册:

键和值只是文本字符串.

所以,不,你不能在hstore中使用hstore作为值.如果你看一下hstore运算符函数,你会发现它们都使用了text值.

我不知道伪造嵌套哈希的任何标准方法.我怀疑你必须构造键(a.b => cfor a => b => c),然后你可以这样的事情:

select slice(doc, array['a.b', 'a.c'])
from my_store
where doc ?& array['a.b', 'a.c']
Run Code Online (Sandbox Code Playgroud)

抓住每个doc具有{b => ..., c => ...}"子哈希" 的"a"切片.

还有一种JSON类型可能更适合您的需求.但是,你必须等待它,我不确定最终的实现会是什么样子.