Django 1.7: Makemigration: non-nullable field

白昼怎懂夜的黑 提交于 2019-12-05 00:57:02

问题


I am trying to use django-orderedmodel (https://github.com/kirelagin/django-orderedmodel) in my project.

Running makemigrations doesn't work:

 You are trying to add a non-nullable field 'order' to slide without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option: 

I would like to know where I'm doing this wrong. Thanks


回答1:


As the order field is unique, you'll need to add the field in several migration steps, replacing the original operations in your migration:

  • Add a nullable field, set the default to NULL.
  • Set the field to a unique value in each row.
  • Add a NOT NULL constraint.

I.e. something like this:

operations = [
    migrations.AddField('myapp.MyModel', 'order', models.PositiveIntegerField(null=True, unique=True)),
    migrations.RunPython(set_order),
    migrations.AlterField('myapp.MyModel', 'order', models.PositiveIntegerField(blank=True, unique=True)),
]

where set_order is a function that sets the order to a valid value, e.g.:

def set_order(apps, schema_editor):
    MyModel = apps.get_model('myapp', 'MyModel')
    for i, model in enumerate(MyModel.objects.all()):
        model.order = i
        model.save()

It's easiest to provide a default value (i.e. 0), and then replace the operations in the generated migration.



来源:https://stackoverflow.com/questions/28012340/django-1-7-makemigration-non-nullable-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!