Django 1.9在迁移时放弃了外键 [英] Django 1.9 drop foreign key in migration

查看:229
本文介绍了Django 1.9在迁移时放弃了外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类示例(models.Model)$ b我有一个Django模型,它有另一个模型的外键: $ b something = models.ForeignKey(SomeModel,db_index = True)

我想保留底层数据库列作为一个字段,但要摆脱数据库中的外键约束。

所以模型将改变为:

  class示例(models.Model):
something_id = models.IntegerField()

显然, something_id 是Django为外键字段创建的列。 b
$ b

我不想删除列并重新创建它(这是Django在更改模型之后自动生成迁移时所做的工作)。

我想保留字段,但是我想通过迁移来删除数据库中的外键约束。我不清楚如何通过Django迁移来实现这一点 - 是否有一些内置的支持它,或者我必须运行一些原始的SQL,如果是这样,我怎样编程获取约束的名称?

解决方案

这是我设法做到的,它基于上面的nimasmi的答案:

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

#这些*将*影响数据库!
database_operations = [
migrations.AlterField(
model_name ='Example',
name ='something',
field = models.ForeignKey('Something',db_constraint = False,db_index = True,null = False)
),
]

#这些*不会影响数据库,它们只会更新Django状态*!
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

]


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?

解决方案

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

这篇关于Django 1.9在迁移时放弃了外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆