I am beginner in postgres. while working on a dummy project I came across this problem.
I have two tables let say t1 and t2.The t1 having 1->Many relation with t2.
I am trying to write an SQL statement which first inserts data in t1 and using id from t1 inserts the multiple rows in t2.
Something like this.
WITH ins AS (
INSERT INTO t1(t1_col)
VALUES (4)
RETURNING t1_id
)
INSERT INTO t2(t1_id, t2_col) VALUES (ins.t1_id, 3), (ins.t1_id, 4)...
t1 structure -> (t1_id primary_key serial, t1_col integer).
t2 structure -> (t2_id primary_key serial, t1_id integer, t2_col integer).
What is the correct way to do this.
Thank you in advance.