Skip to content
Advertisement

Copy data on parent and child table

enter image description here

I have two database tables. What I need to do is to copy specific data from one storage to another, but also keep the mapping to the photos. First part I can do easily writing

but after that newly inserted items does not have photos. Note, that document_id field is unique for not copied items.

Advertisement

Answer

Assuming id columns are auto-generated surrogate primary keys, like a serial or IDENTITY column.

Use a data-modifying CTE with RETURNING to make do with a single scan on each source table:

CTE sel reads all we need from table items.

CTE ins_item inserts into table item. The RETURNING clause returns newly generated id values, together with the (unique!) document_id, so we can link back.

Finally, the outer INSERT inserts into item_photo. We can select matching rows after linking back to the original item.id.

Related:

But:

document_id field is unique for not copied items.

Does that guarantee we are dealing with unique document_id values?

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement