gpt4 book ai didi

python - 卸载应用程序 A,因为应用程序 B 在一次旧迁移中具有依赖项

转载 作者:行者123 更新时间:2023-12-01 02:53:29 24 4
gpt4 key购买 nike

我正在尝试卸载名为 django-cities 的应用程序,但在我的应用程序“places”中,我有一个名为 Venue 的模型,该模型在迁移 0001_initial 中.py 有一个 ForeingKeydjango-citiescities.Subregion 模型。

我继续删除 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 文件,我也在 google 上搜索了此错误并发现 this ,但它没有提供答案。

有这方面的信息吗?

感谢并为我糟糕的英语感到抱歉。

最佳答案

有两种可能的解决方案:

注意:对于以下两种解决方案,您需要先从数据库中删除旧的 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

    缺点:比较复杂且容易出错。

关于python - 卸载应用程序 A,因为应用程序 B 在一次旧迁移中具有依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44489342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com