I'm trying to create some entries by using an INSERT ... SELECT ..., but also need to use the primary key from the 1st INSERT operation to be included as part of a secondary operation:
WITH dogEntries as (INSERT INTO dog (id, another_id, name, date_created)
SELECT public.uuid_generate_v4(), 'efd55343', name, date_created
FROM dog WHERE owner_id = '8921571' RETURNING id as dog_uuid)
INSERT INTO dog_toys (dog_id, bed_id, status)
SELECT (SELECT dog_uuid FROM dogEntries), bed_id, status FROM dog_toys
WHERE dog_id IN(SELECT dog_id FROM dogs WHERE another_id = '21571');
public.uuid_generate_v4() will generate the id column.
But I'm getting an error, not sure but I guess is because the WITH dogEntries is of course returning multiple entries.
The error:
[21000] ERROR: more than one row returned by a subquery used as an expression
Is there any alternative of returning and iterate over the dog table inserted rows, or maybe a hint on how to traverse each of the entries from the WITH dogEntries statement?
But I'm getting an error... So show the verbatim error message. And always your version of Postgres. And table definitions. Ispublic.uuid_generate_v4()supposed to go intoidoranother_id?CREATE TABLEstatements. Then we know where multiple rows can originate. Seems like your owners can have multiple dogs. And tell us what the statement is supposed to achieve exactly. Is it supposed to make a single entry in each table?