Let's say I have two tables: Customer
and City
. There are many Customer
s that live in the same City
. The cities have an uid
that is primary key. The customers have a foreign key reference to their respective city via Customer.city_uid
.
I have to swap two City.uid
s with one another for external reasons. But the customers should stay attached to their cities. Therefore it is necessary to swap the Customer.city_uid
s as well. So I thought I first swap the City.uid
s and then change the Customer.city_uid
s accordingliy via an UPDATE
-statement. Unfortunately, I can not do that since these uids
are referenced from the Customer
-table and PostgreSQL prevents me from doing that.
Is there an easy way of swapping the two City.uid
s with one another as well as the Customer.city_uid
s?
One solution could be:
BEGIN;
1. Drop foreign key
2. Make update
3. Create foreign key
COMMIT;
Or:
BEGIN;
1. Insert "new" correct information
2. Remove outdated information
COMMIT;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With