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.

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.

I wrote a management command for my environment (django).

http://djangosnippets.org/snippets/2691/

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.