Update primary key

primary-key

I want to update a primary key column, but the change should cascade to all other child tables where this primary key is used as foreign key.

Example: table1 has column "slug" as primary key. One row has the slug "foo" which should be "bar". The table "table1" is used in ten other tables as foreign key (child tables).

I is quite easy to create do it like this:

begin;
update table1 ...
update table_child1 ...
update table_child2 ...
...
commit;

Is there a tool to make this easier? For example a tool which looks at the database layout, and creates the update commands.

I use PostgreSQL 9.1, but a portable solution is prefered.

Update: I dont' want/can change the database schema to include "on update cascade" at db level.

Best Answer

If your FOREIGN keys are defined with the ON UPDATE CASCADE option, then you need to do nothing more that update the parent table.

But it seems that the ON UPDATE options are not yet properly implemented in Django.