Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. error: field name must not be null`.
  2.  
  3. with secs as (
  4. select p.page_id, p.domain, s.section_id as sid, s.title as title
  5. from pages p
  6. left join sections s on p.page_id = s.page_id
  7. where p.domain = 'bar.com'
  8. ),
  9. txt as (
  10. select
  11. sid,
  12. json_agg(
  13. json_build_object(
  14. 'Pos', pos,
  15. 'Text', content
  16. )
  17. order by pos asc
  18. ) as txts
  19. from texts
  20. join secs on sid = section_id
  21. group by sid
  22. ),
  23. img as (
  24. select
  25. sid,
  26. json_agg(
  27. json_build_object(
  28. 'Pos', pos,
  29. 'Image', image
  30. )
  31. order by pos asc
  32. ) as imgs
  33. from images
  34. join secs on sid = section_id
  35. group by sid
  36. )
  37. select
  38. json_build_object(
  39. 'ID', s.page_id,
  40. 'Domain', domain,
  41. 'Sections', json_object_agg ( -- Error occurs here
  42. s.title,
  43. json_build_object(
  44. 'ID', s.sid,
  45. 'Texts', t.txts,
  46. 'Images', i.imgs
  47. )
  48. order by s.sid asc
  49. )
  50. )
  51. from secs s
  52. left join txt t on s.sid = t.sid
  53. left join img i on s.sid = i.sid
  54. group by s.page_id, domain;
  55.  
  56. create table pages (
  57. page_id serial primary key,
  58. domain text unique not null
  59. );
  60.  
  61. create table sections (
  62. section_id serial primary key,
  63. title text not null,
  64. page_id int references pages
  65. );
  66.  
  67. create table texts (
  68. section_id int references sections,
  69. pos int not null,
  70. content text not null,
  71. primary key (section_id, pos)
  72. );
  73.  
  74. create table images (
  75. section_id int references sections,
  76. pos int not null,
  77. image text not null,
  78. primary key (section_id, pos)
  79. );
  80.  
  81. -- spanac.com will have 3 sections with texts and images in each, various amounts
  82. insert into pages (domain) values ('spanac.com');
  83. -- foo.com has 1 empty section
  84. insert into pages (domain) values ('foo.com');
  85. -- bar.com has no sections
  86. insert into pages (domain) values ('bar.com');
  87.  
  88. -- spanac.com
  89.  
  90. with s as (
  91. insert into sections (page_id, title) select page_id, 'first' from pages where domain = 'spanac.com' returning section_id
  92. ),
  93. t1 as (
  94. insert into texts (section_id, pos, content) select section_id, 1, 'spanac one.one' from s
  95. ),
  96. t2 as (
  97. insert into texts (section_id, pos, content) select section_id, 2, 'spanac one.two' from s
  98. ),
  99. i1 as (
  100. insert into images (section_id, pos, image) select section_id, 1, 's11.jpg' from s
  101. )
  102. insert into images (section_id, pos, image) select section_id, 2, 's12.jpg' from s;
  103.  
  104. with s as (
  105. insert into sections (page_id, title) select page_id, 'second' from pages where domain = 'spanac.com' returning section_id
  106. ),
  107. t1 as (
  108. insert into texts (section_id, pos, content) select section_id, 1, 'spanac two.one' from s
  109. ),
  110. t2 as (
  111. insert into texts (section_id, pos, content) select section_id, 2, 'spanac two.two' from s
  112. ),
  113. i1 as (
  114. insert into images (section_id, pos, image) select section_id, 1, 's21.jpg' from s
  115. )
  116. insert into images (section_id, pos, image) select section_id, 2, 's22.jpg' from s;
  117.  
  118. with s as (
  119. insert into sections (page_id, title) select page_id, 'third' from pages where domain = 'spanac.com' returning section_id
  120. ),
  121. t1 as (
  122. insert into texts (section_id, pos, content) select section_id, 1, 'Spanac three.one' from s
  123. )
  124. insert into images (section_id, pos, image) select section_id, 1, 's31.jpg' from s;
  125.  
  126. -- foo.com
  127.  
  128. insert into sections (page_id, title) select page_id, 'empty' from pages where domain = 'foo.com';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement