Refactor foreign key to fields

前端 未结 2 1953
庸人自扰
庸人自扰 2020-12-07 02:46

In PostgreSQL I need to refactor a table (Purchases); it has a foreign key to another table (Shop). Instead I want two fields that keep the relatio

2条回答
  •  天涯浪人
    2020-12-07 03:25

    So it seems to me the piece you are missing is simply updating your purchases table to contain the information from your shop table. If that is correct then you could just update the table using the existing foreign key before you drop it:

    UPDATE purchases SET (shop, shop_user) =
        (SELECT name, user FROM shop
         WHERE shop.id = purchases.shop_id);
    

提交回复
热议问题