卸载应用程序A应用程序B在一次旧迁移中具有依赖关系

时间:2017-06-11 22:30:27

标签: python django django-models django-migrations

我正在尝试卸载名为django-cities的应用,但在我的应用中#34;地点"我有一个名为Venue的模型,在迁移0001_initial.py中有一个ForeingKeycities.Subregion的{​​{1}}模型。

我继续删除django-cities的{​​{1}},但我收到以下错误:

django-cities

然后我删除了这些依赖项并卸载INSTALLED_APPS并且一切都为我工作,但是如果其他人必须安装项目,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')), 文件,我也搜索了此错误并找到this,但它没有提供答案。

关于此的任何信息?

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

1 个答案:

答案 0 :(得分:1)

有两种可能的解决方案:

注意:对于以下两种解决方案,您需要先从数据库中删除旧的Venue表,然后再继续。

简单的:

  • 转到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'
                  ))
    
    来自Venue表字段迁移的

  • INSTALLED_APPS

  • 删除您的应用
  • 运行python manage.py migrate

    缺点:这很复杂,容易出错。