I am trying to have a reproducible way to populate a database with some test data.
Suppose (an overly simplified example with) three base tables: name, city, job, and two relationship tables: name-city, and name-job. I need to create an entry into each of three base tables and use the aforementioned entries to create entries in the TWO relationship tables.
What I have already is a way of using a series of with
queries to create entries in the 3 base tables and insert values into ONE relationship table.
with x as
(INSERT INTO "public"."name" VALUES(DEFAULT) RETURNING "id"),
y as
(INSERT INTO "public"."job" VALUES(DEFAULT) RETURNING "id"),
z as
(INSERT INTO "public"."city" VALUES(DEFAULT) RETURNING "id")
INSERT INTO "public"."name-job"("name", "job")
select x.id, y.id from x,y;
I would really like to add a second insert statement
INSERT INTO "public"."name-city"("name", "city")
select x.id, z.id from x,z;
after the first insert but don't know how. I have tried separating the two insert statements with commas, and enclosing them in parentheses and then separating them with commas, and some other ways but nothing works.
It is not entirely an option to use a brand new statement with the second insert because I need the same values of x, y, z to be used. Given my lack of expertise/experience, it is entirely possible I am missing something obvious… so any ideas on how I can do this, ideally, without highly sophisticated tools, would be most welcome.
FWIW, I'm using Postgres (10.x)
Best Answer
You need to put the
INSERT INTO "name-job"
into another CTE. You could also put the last insert into"name-city"
into a CTE as well, and add aSELECT
to get all the inserted ids (and anything else you need from the inserted rows) back.