I have 3 tables :
CREATE TABLE stage
(
a1 text,
b2 text,
c3 text,
d4 text,
e5 text
);
CREATE TABLE main
(
id bigserial PRIMARY KEY,
a1 text,
b2 text,
c3 text
);
CREATE TABLE secondary (
id bigserial PRIMARY KEY,
mainid bigint,
d4 text,
e5 text,
CONSTRAINT secondary_fkey FOREIGN KEY(mainid) REFERENCES main(id)
);
I want to insert data from stage into main and secondary at once but I'm not quite sure how to do that by reusing generated bigserial in main. I'm trying a with query but I'm getting more rows (exponentially) in secondary than expected.
WITH tmp AS (
INSERT INTO
main (a1, b2, c3)
SELECT
a1,
b2,
c3
FROM
stage RETURNING id
)
INSERT INTO
secondary (mainid, d4, e5)
SELECT
tmp.id,
stage.d4,
stage.e5
FROM
tmp,
stage;
Your problem is the cross join you create in the final
INSERTstatement with theFROM tmp, stage;. If you have 10 rows in the stage table, this will generate 100 rows rathe than the 10 you want.If
(a1, b2, c3)uniquely identity a row instageyou could use them for a proper join condition:If that is not feasible (because there are duplicates) you can generate the new IDs for the
maintable before the insert usingnextval()