Drop foreign key, add another - keep data

Skip to first unread message

Peter Edström

Nov 30, 2012, 6:56:11 AM11/30/12
to django...@googlegroups.com

I'd like to drop a ForeignKey-field and add another using south (mysql database), but it won't work. Googling it takes me not far, but it seems to be a problem with dropping foreign key constraints with InnoDB.
What is the recommended way ot dropping a ForeignKey-field, and adding another?

Say the model looks like this:

class Model1(models.Model):
    test = models.ForeignKey(Model2) 

And I won't to change it to:

class Model1(models.Model):
    test2 = models.ForeignKey(Model3) 
Right now I don't really care if I lose data, but for future reference I'd prefer a method that keeps it.

Thank you.

Tom Evans

Nov 30, 2012, 7:23:58 AM11/30/12
to django...@googlegroups.com
How are you trying to do it? There should be several migrations:

1) A schema migration to add the new foreign key, which should be
nullable at this point
2) A data migration to move the old data into the new column
3) A schema migration to drop the nullability of the new foreign key
4) A schema migration to drop the old foreign key

You may not need as many migrations if you do not need to migrate the
old data over, or if the new foreign key is allowed to be null.


Reply all
Reply to author
0 new messages