So I'm trying to insert a value into a secondary table, get the returned id from the insert, and use it together with some values in a prepared statement I'm writing.The return of the first statement(table2) is an id for a foreign key column in the first table. I want to get the id of table 1 in the end.Something like :
WITH table2ID AS
(
INSERT INTO table2 (value) VALUES ('somevalue') RETURNING id;
)
INSERT INTO table1(table2returnvalue,othervalue) VALUES
(table2ID 'val2') RETURNING id
I can see that I will probably need a transaction as well, because I don't want an isolated in table2 if table1's statement fails for some reason.
Could you please help?