Django 1.9 removes foreign key during migration

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

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

I want to save the base column of the database 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_idthis is the column created by Django for the foreign key field.

I don’t want to drop the column and recreate it (this is what Django does when I automatically generate the migration after changing the model as above).

I want to keep the field, but I want to remove the foreign key constraint in the wrap database. I don’t understand how to do this with Django porting - is there any built-in support for it or do I need to run some raw SQL, and if so, how can I programmatically get the name of the constraint?

+4
source share
2 answers

Here's how I did it, it is 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
        )
    ]
+7
source

See SeparateDatabaseAndState . It allows you to specify part of the Django (state) migration separately from the database part of the migration.

  • Change the field in the model file.
  • Create the migration as usual. You will get something like:

    class Migration(migrations.Migration):
    
        dependencies = [
            ('my_app', '0001_whatever.py'),
        ]
    
        operations = [
            migrations.AlterField(
                model_name='example',
                name='something',
                field=models.CharField(max_length=255, null=True)),
            ),
        ]
    
  • :

    class Migration(migrations.Migration):
    
        dependencies = [
            ('my_app', '0001_whatever.py'),
        ]
    
        state_operations = [
            migrations.AlterField(
                model_name='example',
                name='something',
                field=models.CharField(max_length=255, null=True)),
            ),
        ]
        operations = [
            migrations.SeparateDatabaseAndState(state_operations=state_operations)
        ]
    

, - database_operations, Django , .

: , .

+6

All Articles