删除 class 后 django makemigrations 失败
django makemigrations fails after removing a class
我有这样一个模型:
class States(Enum):
first = 'one'
second = 'two'
choices = (
(States.first, 'First'),
...
)
class MyModel(models.Model):
state = FSMField(choices=choices)
还有另一个模型,如:
class MyOtherModel(models.Model):
new_state = CharField(choices=myapp.choices)
出于各种原因,我删除了 States
枚举并用一堆常量代替它,例如:
FIRST = 'one'
然后我迁移了我的数据库,一切都很顺利。
但是当我稍后 运行 ./manage.py makemigrations
时,我得到一个 `AttributeError: module 'myapp.models' has no attribute 'States'.
追溯这样结束:
File "myapp/migrations/0038_xxx.py", line 12, in <module>
class Migration(migrations.Migration):
File "myapp/migrations/0038_xxx.py", line 36, in Migration
('new_state', models.SmallIntegerField(choices=[(myapp.models.States('one'), 'First'), (myapp.models.States('two'), 'Second')])),
AttributeError: module 'myapp.models' has no attribute 'States'
但是如果我将 States
定义粘贴回我的 models
文件,那么 makemigrations
运行 没问题。
如果旧迁移仍然需要此代码,我应该如何删除它?我有一堆迁移遵循这个 0038
,其中大部分已经应用到生产数据库。
您是否也像这样更改了选择元组以使用新常量?
choices = (
(FIRST, 'First'),
...
)
要删除对这个旧枚举的引用,您可以在迁移中使用搜索和替换将 myapp.models.States('one')
修改为 'one'
。
我有这样一个模型:
class States(Enum):
first = 'one'
second = 'two'
choices = (
(States.first, 'First'),
...
)
class MyModel(models.Model):
state = FSMField(choices=choices)
还有另一个模型,如:
class MyOtherModel(models.Model):
new_state = CharField(choices=myapp.choices)
出于各种原因,我删除了 States
枚举并用一堆常量代替它,例如:
FIRST = 'one'
然后我迁移了我的数据库,一切都很顺利。
但是当我稍后 运行 ./manage.py makemigrations
时,我得到一个 `AttributeError: module 'myapp.models' has no attribute 'States'.
追溯这样结束:
File "myapp/migrations/0038_xxx.py", line 12, in <module>
class Migration(migrations.Migration):
File "myapp/migrations/0038_xxx.py", line 36, in Migration
('new_state', models.SmallIntegerField(choices=[(myapp.models.States('one'), 'First'), (myapp.models.States('two'), 'Second')])),
AttributeError: module 'myapp.models' has no attribute 'States'
但是如果我将 States
定义粘贴回我的 models
文件,那么 makemigrations
运行 没问题。
如果旧迁移仍然需要此代码,我应该如何删除它?我有一堆迁移遵循这个 0038
,其中大部分已经应用到生产数据库。
您是否也像这样更改了选择元组以使用新常量?
choices = (
(FIRST, 'First'),
...
)
要删除对这个旧枚举的引用,您可以在迁移中使用搜索和替换将 myapp.models.States('one')
修改为 'one'
。