未在 dockerized Django 中创建的迁移文件

Migrations files not created in dockerized Django

我对 Django 应用程序中的模型进行了更改,需要将迁移应用到数据库。我是 Docker 的新手,所以这给我带来了问题。我已经能够 makemigrationsmigrate,但是没有创建迁移文件,因此由于缺少迁移文件,我无法将我的代码推送到生产环境。我认为这是因为我 运行ning makemigrations 连接到 Docker 容器。

如果我在未连接到 运行ning 容器的情况下尝试 makemigrations,我会收到以下错误:

Traceback (most recent call last):
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 228, in ensure_connection
    self.connect()
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 205, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 172, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "postgres" to address: Temporary failure in name resolution


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

Traceback (most recent call last):
  File "manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 393, in execute_from_command_line
    utility.execute()
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 387, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/core/management/base.py", line 336, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/core/management/base.py", line 377, in execute
    output = self.handle(*args, **options)
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/core/management/base.py", line 87, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 105, in handle
    loader.check_consistent_history(connection)
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/db/migrations/loader.py", line 292, in check_consistent_history
    applied = recorder.applied_migrations()
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 59, in has_table
    self.connection.cursor()
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 267, in cursor
    return self._cursor()
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 244, in _cursor
    self.ensure_connection()
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 228, in ensure_connection
    self.connect()
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 228, in ensure_connection
    self.connect()
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 205, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 172, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/home/andrew/Desktop/app/venv/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not translate host name "postgres" to address: Temporary failure in name resolution

由于我能够在连接到 运行ning 容器时成功迁移,有没有办法让它为我生成迁移文件,我可以将其添加到 git ?或者我需要弄清楚如何从 Docker 之外 运行 makemigrations 吗?

我会考虑使用绑定装载将容器内的迁移文件夹连接到本地计算机上的文件夹。这样,当您 运行 在容器中使用 makemigraions 命令时,迁移文件也应该反映在本地

https://docs.docker.com/storage/bind-mounts/