gpt4 book ai didi

django - 如何使用Duck Duck船长处理Django数据库迁移?

转载 作者:行者123 更新时间:2023-12-02 20:06:23 25 4
gpt4 key购买 nike

机器: docker 1GB

当我在已经部署的manage.py migrations中尝试captain-definition(成功完成makemigrations之后)时,出现此错误,

可能是因为迁移未在我的git repo中同步,并且django无法检测到没有migrations文件夹的迁移已应用于数据库

鸭子队长鸭子队长似乎破坏了我的docker conatiner中对文件系统的任何更改,因此重新启动后migrations文件夹不可用。

System check identified some issues:

WARNINGS:
Users.CustomUser: (auth.W004) 'CustomUser.phone_number' is named as the 'USERNAME_FIELD', but it is not unique.
HINT: Ensure that your authentication backend(s) can handle non-unique usernames.
Operations to perform:
Apply all migrations: Exams, Users, account, admin, auth, contenttypes, sessions, sites, socialaccount
Running migrations:
Applying Exams.0002_auto_20180309_1911...Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column "owner_id" of relation "Exams_topic" already exists


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 200, in handle
fake_initial=fake_initial,
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/migration.py", line 122, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
field,
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 421, in add_field
self.execute(sql, params)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 117, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "owner_id" of relation "Exams_topic" already exists

船长定义
{
"schemaVersion": 1,
"dockerfileLines": [
"FROM library/python:3.6.4-jessie",

"RUN apt-get update && apt-get install -y binutils libproj-dev gdal-bin",
"RUN mkdir -p /usr/src/app",
"WORKDIR /usr/src/app",

"RUN pip install pipenv --upgrade",
"COPY ./src /usr/src/app",
"RUN pipenv install --deploy --system",

"EXPOSE 80",
"COPY ./src/runserver.sh /usr/local/bin/",
"CMD [\"sh\", \"runserver.sh\"]"
]
}

运行服务器
#!/bin/sh

python manage.py collectstatic --noinput
python manage.py makemigrations <appname>
python mange.py migrate
gunicorn quiz.wsgi --bind=0.0.0.0:80

最佳答案

正如Mazel Tov所建议的那样,解决方案是将您的迁移推送到GIT存储库。

进一步来说,

  • 在开发环境中更改模型
  • 运行makemigrations <app_name>
  • commit +推送
  • 在开发和生产环境上都运行migrate

  • 这是可行的,因为迁移是通过数据库而不是文件系统来跟踪的。

    关于django - 如何使用Duck Duck船长处理Django数据库迁移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49209403/

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