Suppose you have these two tables:
create table referenced (id integer primary key); create table referencer (a integer references referenced (id));
Link table link table links:
=> \d referencer Table "public.referencer" Column | Type | Modifiers --------+---------+----------- a | integer | Foreign-key constraints: "referencer_a_fkey" FOREIGN KEY (a) REFERENCES referenced(id)
Then you insert the value in both:
insert into referenced values (1); insert into referencer values (1); select * from referenced rd inner join referencer rr on rd.id = rr.a ; id | a
Now you want to change the link to on update cascade :
alter table referencer drop constraint referencer_a_fkey, add foreign key (a) references referenced (id) on update cascade;
And update it:
update referenced set id = 2; select * from referenced rd inner join referencer rr on rd.id = rr.a ; id | a
You will now have another problem in the primary key of the table referenced table if the updated identifier already exists. But this will create another question.
UPDATE
This is dangerous, so back up your db first. This should be done as root:
update pg_constraint set confupdtype = 'c' where conname in ( select c.conname from pg_constraint c inner join pg_class referenced on referenced.oid = c.confrelid where referenced.relname = 'referenced' and c.contype = 'f' );
It will change all foreign key constraints in the reference table to on update cascade
source share