I want to add a row to my table recipes, and based on the PK that gets created for that row in my first query, need to update my bridge table recipe_technology. Unfortunately I cannot access my newly created id, probably bc I am not returning anything from the insert statement. The below query does what I want it to do when I hardcode the id ("SELECT 1 id"), but otherwise I am a bit clueless what to do. Any leads would be much appreciated!
WITH query_one AS ( INSERT INTO (a, b, c) VALUES ($1, $2, $3) ) INSERT INTO recipe_technology(recipe_id, technology_id) SELECT 1 id, x FROM unnest(ARRAY[1,2,3]) x`,[a,b,c]
WITH query_one(newid) AS ( INSERT INTO (a, b, c) VALUES ($1, $2, $3) returning id ) INSERT INTO recipe_technology(recipe_id, technology_id) SELECT newid, x FROM query_one, unnest(ARRAY[1,2,3]) t(x);
2.1m questions
2.1m answers
60 comments
57.0k users