重命名模型会破坏迁移历史
Renaming model breakes migration history
我在两个不同的应用程序中有两个模型:
# app1 models.py
class App1Model(models.Model):
pass
# app2 models.py
from app1.models import App1Model
class App2Model(App1Model):
pass
我想重命名 App1Model,然后重新创建 App2Model 以显式 OneToOneField
而不是魔法 app1model_ptr
。因此,我创建了完全删除 App2Model 的迁移(无论出于何种原因,我都不关心数据),它 运行 成功了。然后我在第一个重命名 App1Model 的应用程序中创建迁移,它 运行 也很完美,我用新名称检查了这个 table 和它的所有关系(以及与它的关系),没问题。
然后奇怪的事情发生了:当我在 app2 上 运行 makemigrations
或 migrate
我得到一个错误
django.db.migrations.exceptions.InvalidBasesError: Cannot resolve bases for [<ModelState: 'app2.App2Model'>]
在首次迁移 app2(0001_initial.py 在 app2 迁移中)时创建当前项目状态时失败,其中此模型是通过从 App1Model 继承并使用其旧名称首次创建的。
有什么办法可以解决这个问题吗?在当前状态下 App2Model
已删除,App1Model
已重命名,由于这个问题我无法对 app2 进行任何迁移。
P.S。我使用 Django 1.10.2
刚刚找到解决方案:
需要添加 app2
的最后一次迁移,其中我删除了 App2Model
作为对 app1
迁移的依赖,其中我重命名了 App1Model
,这样项目状态将正确构建命令。实际上错误消息本身有一些与之相关的东西,但我没有抓住要点:
This can happen if you are inheriting models from an app with
migrations (e.g. contrib.auth) in an app with no migrations; see
https://docs.djangoproject.com/en/1.10/topics/migrations/#dependencies
for more
我把它放在这里是为了未来的我和那些将遭受类似事情的人。
我在两个不同的应用程序中有两个模型:
# app1 models.py
class App1Model(models.Model):
pass
# app2 models.py
from app1.models import App1Model
class App2Model(App1Model):
pass
我想重命名 App1Model,然后重新创建 App2Model 以显式 OneToOneField
而不是魔法 app1model_ptr
。因此,我创建了完全删除 App2Model 的迁移(无论出于何种原因,我都不关心数据),它 运行 成功了。然后我在第一个重命名 App1Model 的应用程序中创建迁移,它 运行 也很完美,我用新名称检查了这个 table 和它的所有关系(以及与它的关系),没问题。
然后奇怪的事情发生了:当我在 app2 上 运行 makemigrations
或 migrate
我得到一个错误
django.db.migrations.exceptions.InvalidBasesError: Cannot resolve bases for [<ModelState: 'app2.App2Model'>]
在首次迁移 app2(0001_initial.py 在 app2 迁移中)时创建当前项目状态时失败,其中此模型是通过从 App1Model 继承并使用其旧名称首次创建的。
有什么办法可以解决这个问题吗?在当前状态下 App2Model
已删除,App1Model
已重命名,由于这个问题我无法对 app2 进行任何迁移。
P.S。我使用 Django 1.10.2
刚刚找到解决方案:
需要添加 app2
的最后一次迁移,其中我删除了 App2Model
作为对 app1
迁移的依赖,其中我重命名了 App1Model
,这样项目状态将正确构建命令。实际上错误消息本身有一些与之相关的东西,但我没有抓住要点:
This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth) in an app with no migrations; see https://docs.djangoproject.com/en/1.10/topics/migrations/#dependencies for more
我把它放在这里是为了未来的我和那些将遭受类似事情的人。