【发布时间】:2021-10-25 19:32:54
【问题描述】:
我正在使用 Dokku 将我的 Django 项目部署到 VPS。我的项目使用CustomUser 模型,并且项目中有两个应用程序:accounts 具有 CustomUser 和 gradebook。
在部署期间,该过程会运行 makemigrations 和 migrate。部署后,当我运行 python manage.py showmigrations 时,我得到以下信息:
account
[X] 0001_initial
[X] 0002_email_max_length
accounts
(no migrations)
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
[X] 0010_alter_group_name_max_length
[X] 0011_update_proxy_permissions
[X] 0012_alter_user_first_name_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
gradebook
(no migrations)
sessions
[X] 0001_initial
sites
[X] 0001_initial
[X] 0002_alter_domain_unique
account 来自django-allauth。
看起来我的应用程序没有被迁移。所以我这样做:
me@myserver:/home$ dokku run gradebook python manage.py makemigrations accounts
success
Migrations for 'accounts':
accounts/migrations/0001_initial.py
- Create model CustomUser
me@myserver:/home$
看起来事情已经准备好迁移了。然后我运行 migrate 并得到一个错误django - ValueError: Dependency on app with no migrations: accounts:
shmish@Henry:/home$ dokku run gradebook python manage.py migrate accounts
success
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/loader.py", line 174, in check_key
return self.graph.root_nodes(key[0])[0]
IndexError: list index out of range
During handling of the above exception, another exception occurred:
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.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.8/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.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
self.build_graph()
File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/loader.py", line 235, in build_graph
self.add_external_dependencies(key, migration)
File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/loader.py", line 199, in add_external_dependencies
parent = self.check_key(parent, key[0])
File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/loader.py", line 181, in check_key
raise ValueError("Dependency on app with no migrations: %s" % key[0])
ValueError: Dependency on app with no migrations: accounts
2021/08/25 16:19:16 exit status 1
此时我无法迁移我的任何一个应用程序。我对此感到奇怪的另一件事是,我仍然能够createsuperuser,进入 shell,from accounts.models import CustomUser 并成功查询 CustomUser。
此错误类似于:ValueError: Dependency on app with no migrations: account 但我没有看到解决方案。
下一步 我在我的开发和生产服务器上重新创建了我的数据库。在开发中,我重新运行了初始迁移,然后提交到我的存储库。然后我部署到我的生产服务器。迁移似乎有效,但我无法通过登录和注册表单。我无法确定确切的错误是什么,但页面超时并显示一条消息“我们很抱歉,但出了点问题。”我认为这是一条 nginx 消息。
在尝试部署几次以解决问题后,我收到了新的迁移错误:
Your models in app(s): 'account' have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
这是一个无休止的 makemigrations 循环,似乎可以工作,然后迁移,然后错误又出现了。
注意 - 我可以进入 shell 并手动创建一个 CustomUser 对象,但我无法通过网站首页上的表单。
现在看起来就像 2015 年的错误:Django Heroku Error "Your models have changes that are not yet reflected in a migration"
所有这些在我的开发环境中都可以正常工作,最大的区别在于它是 Docker 而 prod 是 Dokku。
【问题讨论】:
-
正常的工作流程是在部署之前创建迁移并将它们签入您的存储库。在像 heroku 和 dokku 这样的系统上,文件系统是短暂的,所以你不能依赖你保存在那里的文件(例如媒体文件,在这种情况下是迁移文件)以后会在那里。最后一条评论 - 我会避免在部署过程中自动运行迁移。随着您的项目变得越来越复杂,您可能需要更好地控制迁移的运行时间。
-
如果迁移文件是临时的,如果不通过部署我们如何运行迁移?我们是否手动将迁移文件上传到某处的服务器,然后运行迁移?理想情况下,我打算拥有一个在初始部署后不需要任何迁移并将它们检入存储库的数据库。
-
迁移文件不是临时的,而是像 Heroku 这样的系统上的文件系统。你通常会在你的开发环境中运行
makemigrations。将accounts/migrations/0001_initial.py提交到您的存储库,然后进行部署。然后你应该可以用 dokku 运行migrate。 -
如果
CustomUser表已经创建,那么您的迁移和数据库不同步。您也许可以运行manage.py migrate accounts --fake-initial(docs),但如果可能的话,从新数据库开始是最安全的。 -
我在开发环境中运行我的
makemigrations,提交,然后部署。我已经多次重新创建数据库(和应用程序)。我不再收到上述错误,但我收到“您的应用程序中的模型:‘帐户’的更改尚未反映在迁移中”。我会编辑这个问题,因为我相信这都是相关的。