У меня есть секвенционный код в plpg sql, скорее это будет функция после решения этой проблемы:
do
$$
declare
...
begin
for new_element in select * from json_array_elements(datajson)
loop
insert into entities (id, "attributes") values (default, (new_element->'attributes')::jsonb) returning id into new_element_id;
raise notice 'new: %', new_element_id;
for team_element in select * from json_array_elements((new_element->'team_accounts_reverse')::json)
loop
team_aspects := team_element->'aspects';
team_account := team_element->'account';
if not team_account::jsonb ? 'id' then
insert into entities (id, "attributes") values (default, (team_account->'attributes')::jsonb) returning id into new_account_id;
else
new_account_id := team_account->'id';
end if;
insert into junctions (source_id, target_id, aspects) values (new_account_id::uuid, new_element_id::uuid, team_aspects::jsonb);
end loop;
for attachment_element in select * from json_array_elements((new_element->'attachment_users_direct')::json)
loop
attachment_aspects := attachment_element->'aspects';
attachment_file := attachment_element->'file';
if not attachment_file::jsonb ? 'id' then
insert into entities (id, "attributes") values (default, (attachment_file->'attributes')::jsonb) returning id into new_file_id;
else
new_file_id := attachment_file -> 'id';
end if;
insert into junctions (source_id, target_id, aspects) values (new_element_id::uuid, new_file_id::uuid, attachment_aspects::jsonb);
end loop;
end loop;
commit;
end;
$$
language plpgsql;
, почему pg выдает ошибку Unterminated dollar quote started at position 5 in SQL ...
только после добавления второго вложенного l oop?