Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.9 drop foreign key in migration

I have a Django model that has a foreign key to another model:

class Example(models.Model)
   something = models.ForeignKey(SomeModel, db_index=True)

I want to keep the underlying DB column as a field, but to get rid of the foreign key constraint in the database.

So the model will change to:

class Example(models.Model):
   something_id = models.IntegerField() 

And, to be clear, something_id is the column that Django had created for the foreign key field.

I do not want to drop the column and re-create it (this is what Django does when I auto-generate migrations after changing the model as above).

I want to keep the field but I want to remove the foreign key constraint in the database with a migration. It's not clear to me how to do this with a Django migration - is there some built in support for it or do I have to run some raw SQL and, if so, how do I programatically get the name of the constraint?

like image 908
J. Doe Avatar asked Jul 11 '16 16:07

J. Doe


People also ask

How do I drop a foreign key in migration?

But we can't remove directly using dropColumn() because we did apply foreign key constraint so we should drop foreign key constraint of that column using dropForeign() and then we can delete column using dropColumn().

How do I fix migration issues in Django?

The first step would be to reverse both the migrations using Django migrate command. python manage.py migrate books 0002 would simply reverse all the migrations which were performed after 0002 step. You can verify this by opening the django_migrations table.


1 Answers

This is how I managed to do it, it's based on nimasmi's answer above:

class Migration(migrations.Migration):
    dependencies = [
        ('my_app', '0001_initial'),
    ]

    # These *WILL* impact the database!
    database_operations = [
        migrations.AlterField(
            model_name='Example',
            name='something',
            field=models.ForeignKey('Something', db_constraint=False, db_index=True, null=False)
        ),
    ]

    # These *WON'T* impact the database, they update Django state *ONLY*!
    state_operations = [
        migrations.AlterField(
            model_name='Example',
            name='something',
            field=models.IntegerField(db_index=True, null=False)
        ),
        migrations.RenameField(
            model_name='Example',
            old_name='something',
            new_name='something_id'
        ),
    ]

    operations = [
        migrations.SeparateDatabaseAndState(
            database_operations=database_operations,
            state_operations=state_operations
        )
    ]
like image 178
J. Doe Avatar answered Oct 01 '22 08:10

J. Doe