Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

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.

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

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

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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