卸载应用程序 A,应用程序 B 在一个旧迁移中具有依赖性

Uninstall app A that app B have a dependency in one old migration

我正在尝试卸载名为 django-cities 的应用程序,但在我的应用程序 "places" 中,我有一个名为 Venue 的模型,在迁移 0001_initial.py 中有一个 ForeingKeycities.Subregion 型号 django-cities

我继续删除 INSTALLED_APPSdjango-cities,但出现以下错误:

Traceback (most recent call last):
  File "/home/d/.virtualenvs/beplay/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/home/d/.virtualenvs/beplay/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 128, in inner_run
    self.check_migrations()
  File "/home/d/.virtualenvs/beplay/local/lib/python2.7/site-packages/django/core/management/base.py", line 422, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/home/d/.virtualenvs/beplay/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/home/d/.virtualenvs/beplay/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 52, in __init__
    self.build_graph()
  File "/home/d/.virtualenvs/beplay/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 274, in build_graph
    raise exc
django.db.migrations.exceptions.NodeNotFoundError: Migration places.0001_initial dependencies reference nonexistent parent node (u'cities', u'0010_adjust_unique_attributes')

然后我删除了那些依赖项并卸载了 django-cities 并且所有这些都对我有用,但是如果其他人必须安装该项目,migrate 命令会引发以下错误:

ValueError: Related model u'cities.Subregion' cannot be resolved

因为我从requirements.txt中删除了它并且在迁移中仍然被引用0001_initial.py:

class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Venue',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')),
                ('modified', model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified')),
                ('name', models.CharField(max_length=255)),
                ('phone', models.CharField(blank=True, max_length=255, null=True)),
                ('mobile', models.CharField(blank=True, max_length=255, null=True)),
                ('email', models.EmailField(blank=True, max_length=254, null=True)),
                ('address', models.CharField(blank=True, max_length=255, null=True)),
                ('latitude', models.CharField(blank=True, max_length=100, null=True)),
                ('longitude', models.CharField(blank=True, max_length=100, null=True)),
                ('subregion', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='cities.Subregion')),
            ],
            options={
                'abstract': False,
            },
        ),
    ]

然后我删除行:

('subregion', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='cities.Subregion')),

还有另一个错误:

AttributeError: 'NoneType' object has no attribute 'is_relation'

我也尝试从项目中删除所有 .pyc 文件,我也用谷歌搜索了这个错误并找到了 this,但没有提供答案。

有这方面的信息吗?

谢谢,抱歉我的英语不好。

有两种可能的解决方案:

注意:对于以下两种解决方案,您需要在继续之前从数据库中删除旧的 Venue table。

简单的:

  • 转到您的 migrations/ 文件夹并删除除 __init__.py 文件.

  • INSTALLED_APPS 中删除您的应用。

  • 运行 python manage.py makemigrations 这将在文件夹中重新创建您的迁移。

  • 运行 python manage.py migrate

    缺点: 如果这很重要,您会丢失迁移历史记录(在您的情况下,我认为这无关紧要,因为您指的是迁移 0001

困难的方法:

您将需要修改 migrations/ 文件夹中的每个迁移文件:

  • 转到每个迁移文件并找到对将要卸载的应用程序的任何引用
  • 删除那些引用:

    示例删除行:

    ('subregion', models.ForeignKey(
                      blank=True, 
                      null=True,
                      on_delete=django.db.models.deletion.CASCADE,
                      to='cities.Subregion'
                  ))
    

    从会场table场迁移。

  • INSTALLED_APPS 中删除您的应用。

  • 运行 python manage.py migrate

    缺点:操作复杂,容易出错