Django 1.9 drop foreign key in migration

前端 未结 3 840
Happy的楠姐
Happy的楠姐 2020-12-30 09:13

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

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


        
3条回答
  •  不知归路
    2020-12-30 09:28

    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
            )
        ]
    

提交回复
热议问题