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

前端之家收集整理的这篇文章主要介绍了是否可能,Postgresql的HStore类型中嵌套哈希的语法是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我甚至不确定Postgres的HStore数据类型可以包含嵌套散列,如果可以的话,如何插入它们?

这是我迄今为止所尝试过的:

  1. -- Database: test1
  2.  
  3. -- DROP DATABASE test1;
  4. /*
  5. CREATE DATABASE test1
  6. WITH OWNER = iainuser
  7. ENCODING = 'UTF8'
  8. TABLESPACE = pg_default
  9. LC_COLLATE = 'en_GB.UTF-8'
  10. LC_CTYPE = 'en_GB.UTF-8'
  11. CONNECTION LIMIT = -1;
  12. */
  13. /* create extension hstore; */
  14. /*drop table my_store;*/
  15. /*
  16. create table my_store (
  17. id serial primary key not null,doc hstore
  18. );
  19.  
  20. CREATE INDEX my_store_doc_idx_gist
  21. ON my_store
  22. USING gist
  23. (doc);
  24. */
  25. /* select doc from my_store; */
  26. /*
  27. insert into my_store (doc) values ( '"a" => "1"' );
  28. select doc -> 'a' as first_key from my_store; -- returns "1"
  29. */
  30.  
  31. /* insert into my_store (doc) values ( '"b" => "c" => "3"' ); -- doesn't work */
  32. /* insert into my_store (doc) values ( '"b" => ("c" => "3")' ); -- doesn't work */
  33. /* insert into my_store (doc) values ( '"b" => hstore("c" => "3")' ); -- doesn't work */
  34. /* insert into my_store (doc) values ( '"b"' => hstore("c" => "3")' ); -- doesn't work */
  35. /* insert into my_store (doc) values ( "b"=>'"c"=>"3"'::hstore ); -- doesn't work */

如果不可能的话,现在可以使用嵌套散列函数来接受标准/成语 – 也许将它们分开并使用id来引用它们?

对此的任何帮助将不胜感激.

fine manual

Keys and values are simply text strings.

所以,不,你不能在商店里使用一个商店作为价值.如果你看看hstore operatorsfunctions,你会看到他们都使用文本值.

我不知道任何标准的方法来伪装嵌套哈希.我怀疑你必须构造密钥(a.b => c for a => b => c),那么你可以这样做:

  1. select slice(doc,array['a.b','a.c'])
  2. from my_store
  3. where doc ?& array['a.b','a.c']

抓取每个文档的“a”片,其中包含{b => …,c => …}“sub-hash”.

还有一个JSON type可能更适合您的需求.但是,你必须等待它,我不知道what the final implementation will look like.

猜你在找的Postgre SQL相关文章