I'm using Postgres. I have an old table and a new table. The records in the old table need to have related records in the new table. The new table records are effectively going to act as the parents for records in the new table.
I'm trying to write a migration where each "child" from the old table will have "parent" in the new table.
OLD TABLE (I've added a foreign key in anticipation of populating it as part of the migration)
id | name | new_id (FK)
----+------+-------------
1 | 1st |
2 | 2nd |
NEW TABLE
id | name
----+------
|
I need to do the following:
SELECT
all records from the old tableINSERT
a record into the new table for each old record andRETURNING id
UPDATE
the old record's foreign key value with theRETURNING id
Is there a way to accomplish this using a set query? Or do I need to start delving into Postgres specific things like LOOP
s?