【问题标题】:Django migration: Revert or bypass "ValueError: Dependency on app with no migrations"Django 迁移:恢复或绕过“ValueError:依赖于没有迁移的应用程序”
【发布时间】:2021-12-28 08:49:25
【问题描述】:

在尝试分离不同应用中的模型时,我遇到了一个需要支持的问题:

我有 2 个模型:

class SeriesCompletion(models.Model):
   series = models.ForeignKey(
       'Series', blank=True, null=True, on_delete=models.SET_NULL, )
   user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True,
                         null=True, on_delete=models.SET_NULL,)

class Series(models.Model):
   ...
   users_who_completed = models.ManyToManyField(
     settings.AUTH_USER_MODEL, through='app1.SeriesCompletion',)

它们都位于app1

这个想法(在开发环境中运行良好)是做一个 dumpadata,将 SeriesCompletion 移动到 app2 并进行迁移,而不是加载数据以正确重新填充

但是,当迁移到 prod 环境时,我使用 Series 模型运行了 app1 迁移,并引用了 app2

class Series(models.Model):
       ...
       users_who_completed = models.ManyToManyField(
         settings.AUTH_USER_MODEL, through='app2.SeriesCompletion',)

它在 prod 中传递,并且在为 app2 执行 makemigration 时,由于循环引用而阻塞:ValueError: <function ManyToManyField.contribute_to_class.<locals>.resolve_through_model at 0x7f3e531f4400> contains a lazy reference to app2.seriescompletion, but app 'app2' isn't installed.

从那以后我尝试了很多事情,但我总是被阻止,我无法继续为app1app2 进行迁移......或者向后移动返回之前使用app1 进行的迁移 在预迁移/预makemigrations 检查期间,我总是以错误消息结束:

ValueError: Dependency on app with no migrations

有什么好主意可以帮助我摆脱困境吗?

【问题讨论】:

    标签: django django-models django-migrations


    【解决方案1】:

    好吧,我们花了很多时间,但我们终于能够摆脱这个烂摊子了。

    事实上,我们在一些旧迁移中的某处有一个migrations.RunPython(populate_SeriesCompletion) 操作,它当然是指SeriesCompletion 模型......并且是混乱的根源

    由于模型演变时类似的migrations.RunPython 操作,我偶尔会遇到追溯兼容性问题。

    一段时间以来,我一直在努力避免在我的迁移中使用 migrations.RunPython 操作。我最初认为使用它们是一种避免“在翻译中丢失”的好习惯……但事实证明,这似乎是一种不好的做法。

    【讨论】:

      猜你喜欢
      • 2021-10-25
      • 2017-10-07
      • 2020-10-20
      • 2016-04-14
      • 2019-07-27
      • 2023-04-06
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多