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.

link|improve this question
feedback

2 Answers

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.

link|improve this answer
No, there is no "ON UPDATE CASCADE", and I can't add it. – guettli Feb 22 at 13:27
Yes, you are right, only ON DELETE is supported, my wrong. – ypercube Feb 22 at 13:30
Update: I dont' want/can change the database schema to include "on update cascade" at db level. – guettli Feb 22 at 15:28
feedback

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

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

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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