【问题标题】:Adding label to Django AppConfig gives error with migrations向 Django AppConfig 添加标签会导致迁移错误
【发布时间】:2021-10-05 19:08:33
【问题描述】:

我是 Django 新手。我正在使用 DRF。我试图使用 dj-rest-framework 和 django-allauth 集成社交身份验证,但我目前有一个名为 account 的应用程序,因此它与 allauth.account 存在命名冲突。

我通过关注this solution 设法解决了这个问题。这是allauth.account 的一个已知问题,更改其中一个应用程序标签将解决冲突。

第二个问题来了。我配置了网址后,报错了

ProgrammingError
"Table '<project-name>.socialaccount_socialapp' doesn't exist"

所以,我认为进行迁移可能会解决问题。当我尝试进行迁移时,它根据我所做的更改给出了一些不同的错误,但似乎没有一个解决方案有效。

当我按照the solution mentioned above 中的描述更改 AllAuth 帐户的标签名称,然后尝试进行迁移 python manage.py makemigrations 时出现此错误:

CommandError: Conflicting migrations detected; multiple leaf nodes in the migration graph: (0001_initial, 0002_email_max_length in allauth_account).
To fix them run 'python manage.py makemigrations --merge'

然后运行:python manage.py makemigrations --merge 给出

ValueError: Could not find common ancestor of ['0001_initial', '0002_email_max_length']

如果我尝试更改我的帐户应用标签而不是 allauth 帐户标签,则会出现一些不同的错误。

django.db.migrations.exceptions.NodeNotFoundError: Migration myaccount.0004_auto_20210406_1505 dependencies reference nonexistent parent node ('account', '0003_something')

所以迁移上的应用名称仍然是account,而不是myaccount。运行 python manage.py makemigrations 会出现同样的错误。

手动修改迁移依赖,因此将account更改为myaccount将修复此错误,但是当再次尝试进行迁移时,发生了很多其他错误:

ValueError: The field myaccount was declared with a lazy reference to 'account', but app 'account' doesn't provide model 'team'.
The field <app.something> was declared with a lazy reference to 'account', but app 'account' doesn't provide model 'team'.
The field <app.something> was declared with a lazy reference to 'account', but app 'account' doesn't provide model 'team'.
The field <app.something> was declared with a lazy reference to 'account', but app 'account' doesn't provide model 'team'.
The field <app.something> was declared with a lazy reference to 'account', but app 'account' doesn't provide model 'team'.

【问题讨论】:

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


    【解决方案1】:

    如果您的帐户应用程序已经在数据库中,那么更改应用程序名称需要的不仅仅是这些。您可以按照此问题中描述的步骤执行此操作:[1]:How to change the name of a Django app? 如果您的项目仍在开发中,那么您可以清理所有迁移和数据库,重命名您的应用并重新开始

    【讨论】:

    • 我没有更改应用名称,我只是添加了一个标签,因此它不会发生冲突。这是 Django 1.7 附带的一个特性。该应用程序正在与多个用户一起使用,我真的不想重命名我的帐户应用程序。我想要最简单的解决方案来解决冲突并让一切正常。
    • 我看到了您遇到的问题,因为我对您的方法不满意。很快就提出了其他建议。也许您可以尝试通过从已安装的应用程序中删除您的帐户应用程序并将 app_label 添加到每个模型以指向 allauth 帐户应用程序来组合这两个应用程序。但这可能会对您需要彻底测试的项目产生副作用。但同样,拥有两个不同的名称更简洁、更安全、更易于维护。
    猜你喜欢
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 2017-01-06
    • 2014-12-06
    • 2018-07-20
    • 1970-01-01
    相关资源
    最近更新 更多