好的,所以我有一个名为locationmanager的应用程序,其模型如下所示:
class Location(models.Model):
region = models.ForeignKey(Region)
我想把它改成这个
class Location(models.Model):
region = models.ForeignKey(Region, blank=True, null=True)
更改后,我运行了一个类似于以下内容的模式迁移:
$ python manage.py schemamigration locationmanager --auto
? The field 'Location.region' does not have a default specified, yet is NOT NULL.
? Since you are making this field nullable, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now.
? 2. Specify a one-off value to use for existing columns now
? 3. Disable the backwards migration by raising an exception; you can edit the migration to fix it later
? Please select a choice: 2
? Please enter Python code for your one-off default value.
? The datetime module is available, so you can do e.g. datetime.date.today()
>>> 0
~ Changed field region on locationmanager.Location
? The field 'Location.manager' does not have a default specified, yet is NOT NULL.
? Since you are making this field nullable, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now.
? 2. Specify a one-off value to use for existing columns now
? 3. Disable the backwards migration by raising an exception; you can edit the migration to fix it later
? Please select a choice: 0
! Invalid choice.
? Please select a choice: 2
? Please enter Python code for your one-off default value.
? The datetime module is available, so you can do e.g. datetime.date.today()
>>> 0
~ Changed field manager on locationmanager.Location
Created 0003_auto__chg_field_location_region__chg_field_location_manager.py. You can now apply this migration with: ./manage.py migrate locationmanager
$ sudo python manage.py migrate locationmanager --fake
Running migrations for locationmanager:
- Migrating forwards to 0003_auto__chg_field_location_region__chg_field_location_manager.
> locationmanager:0002_initial
(faked)
> locationmanager:0003_auto__chg_field_location_region__chg_field_location_manager
(faked)
运行测试后,表单向我吐出错误:
IntegrityError at /dash/location/add
locationmanager_location.region_id may not be NULL
我以为我改变了吗?我能做错什么?我目前可以做些什么来解决这个问题?
答案 0 :(得分:0)
您指定--fake
迁移。伪造您的初始迁移以进入当前状态(在更改之前)并定期迁移。
为了修复迁移,您必须使用
回滚到迁移0002./manage.py migrate locationmanager 0002
迁移0002是假的,没关系,因为您可能不希望南方在更改之前更改状态的数据库方案。
现在您可以使用
定期迁移到迁移0003./manage.py migrate locationmanager
请注意,您始终可以使用
查看已应用的迁移列表./manage.py migrate locationmanager --list