【问题标题】:django.db.migrations.exceptions.NodeNotFoundError: Migration accounts.0001_initial dependencies reference nonexistent parent nodedjango.db.migrations.exceptions.NodeNotFoundError: Migration accounts.0001_initial dependencies 引用不存在的父节点
【发布时间】:2021-04-20 21:20:47
【问题描述】:

我正在尝试在 heroku 上部署我的项目,我使用的是 django 3.1,但我无法做到这一点。由于迁移,我收到错误。请我谦虚地请求您给这个问题一些时间来解决这个问题。每当我运行命令 heroku run python manage.py migrate 时,它都会提供以下回溯。

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 92, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/loader.py", line 53, in __init__
    self.build_graph()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/loader.py", line 255, in build_graph
    self.graph.validate_consistency()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/graph.py", line 195, in validate_consistency
    [n.raise_error() for n in self.node_map.values() if isinstance(n, DummyNode)]
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/graph.py", line 195, in <listcomp>
    [n.raise_error() for n in self.node_map.values() if isinstance(n, DummyNode)]
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/graph.py", line 58, in raise_error
    raise NodeNotFoundError(self.error_message, self.key, origin=self.origin)
django.db.migrations.exceptions.NodeNotFoundError: Migration accounts.0001_initial dependencies reference nonexistent parent node ('auth', '0023_remove_user_current_balance')

迁移依赖

 dependencies = [
        ('auth', '0023_remove_user_current_balance'),
    ]

account.model

GENDER_CHOICES = (
    ('male','Male'),
    ('female','Female'),
    )
class User(auth.models.AbstractUser):
    
    current_balance = models.IntegerField(("user balance"),default=0, blank=True, null=True)
    age = models.IntegerField(("age"),blank=True, null=True)
    gender = models.CharField(("gender"), max_length=50,choices=GENDER_CHOICES,null=True)
    nationality = models.CharField(("nationality"), max_length=50,null=True)

    def __str__(self):
        return "@{}".format(self.username)

当我试图注释掉依赖时,它返回给我:raise ValueError('Related model %r cannot be resolve'% self.remote_field.model)ValueError: Related model 'auth.Group' cannot be已解决

如果需要更多代码,请在评论部分告诉我谢谢。

【问题讨论】:

  • 您的迁移历史似乎不一致,您的仓库中是否存在名为 0023_remove_user_current_balance 的迁移
  • @iklinac 是的,但是我已经进行了迁移,之后我得到了 massege 没有迁移,但是当我将代码放在服务器上时,它显示了这个错误
  • @iklinac 现在也出现在本地了
  • @iklinac 我面临着同样的问题。你能帮助我们吗?

标签: python django django-models django-rest-framework django-views


【解决方案1】:

我遇到了同样的问题。这是因为迁移历史不一致。所以我删除了所有已安装应用程序中的迁移目录,然后调用python manage.py makemigrations 命令。新的迁移没有问题。

【讨论】:

  • 这将在您的本地主机上工作,但可能会在生产环境中产生问题,该生产环境包含有关数据库中迁移历史记录的信息。
  • 你是对的。此解决方案仅适用于开发环境。谢谢。
【解决方案2】:

如果您删除了一些过去的迁移,可能会发生这种情况。 所有迁移都会创建一个图(树状结构)。您只需要确保其中没有悬挂的树枝会导致之前的树枝不再属于树的一部分。您可以将其放在纸上以进行可视化。

解决办法是:

  1. 从列表中删除依赖项在分支的情况下

假设附属应用程序被删除(及其迁移)

dependencies = [
    ('affiliate', '0003_remove_affiliateaccount_project'),
    ('partner', '0073_auto_20191008_1705'),
]

解决方案:

dependencies = [
    ('partner', '0073_auto_20191008_1705'),
]
  1. 将其指向不同的端点

    依赖 = [ ('auth', '0023_remove_user_current_balance'), ]

解决方案:

dependencies = [
    ('partner', 'XXXX_head_that_exists_YYYYMMDD_XXXX'),
]

【讨论】:

    猜你喜欢
    • 2021-08-24
    • 2018-08-26
    • 1970-01-01
    • 2015-12-01
    • 2015-04-24
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多