I have a table product with column product_image_path. These parts of the data need to be moved to a related table, image, which has a separate column image_path and a surrogate key id. The tables are related through the foreign key product.image_id. Is there a way to insert all values of product_image_path into the image table and immediately update the product.image_id to refer to the rows that were just created?
This has to be done in SQL or PL/pgSQL as it is part of a series of database migrations, which do not allow arbitrary scripting.
product:
id | product_image_path
----+-------------------
1 | foo.jpg
2 | bar.jpg
3 | foo.jpg
Should become:
product:
id | image_id
---+---------
1 | 1
2 | 2
3 | 3
image:
id | image_path
---+-----------
1 | foo.jpg
2 | bar.jpg
3 | foo.jpg