Django:迁移取决于已删除的第 3 方模块

Django: Migrations depend on removed 3rd-party module

在我的 django 项目中,我一直在使用 django-taggit 为模型添加标记功能。

添加标签的迁移还列出了初始 taggit 迁移作为依赖项:

dependencies = [
    ('taggit', '0001_initial'),
    # …
]

稍后,我删除了所有地方的 taggit,包括 INSTALLED_APPS

问题是 django 无法解析属于 taggit 的迁移并引发错误。

在这种情况下,首选解决方案是什么?

我能想到一个两步走的策略:

  1. INSTALLED_APPS 中保留 taggit,直到所有服务器 运行 项目都是最新的
  2. 之后压缩迁移,以便该字段不再显示,然后才从 INSTALLED_APPS
  3. 中删除 taggit

说得对。请注意,在创建压缩迁移之前,您不必等待所有服务器都是最新的。来自 the documentation:

These files are marked to say they replace the previously-squashed migrations, so they can coexist with the old migration files, and Django will intelligently switch between them depending where you are in the history.

对于最后一步,您甚至可以删除旧的迁移文件,因此您的源代码中的任何地方都不会再提及 taggit

You must then transition the squashed migration to a normal initial migration, by:

  • Deleting all the migration files it replaces

  • Removing the replaces argument in the Migration class of the squashed migration (this is how Django tells that it is a squashed migration)