我在开发机器上工作。代码在开发期间在docker容器中进行测试,因为:
docker-compose.yml
postgres
):我只想在我的开发机器应用程序中进行开发,而不是用于测试或运行应用程序。现在我正在尝试更新我的迁移:
» DJANGO_SECRET_KEY=xxx python manage.py makemigrations
....
django.db.utils.OperationalError: could not translate host name "postgres" to address: No address associated with hostname
嗯,是的,当然,我的主机无法看到postgres
容器(根据docker compose架构,只有正在运行的容器可以看到对方)
我可以连接到应用容器,在那里进行迁移,并检索迁移到我的开发机器,但这似乎不是一个好的解决方案。
我的理解是迁移的计算基于:
我不明白为什么我需要一个数据库实例来进行迁移。
我可以在没有数据库连接的情况下进行迁移吗?怎么样?
在下面添加完整的追溯。从我所看到的,vanilla makemigrations
命令尝试连接到数据库。为什么会这样?
我正在跑步:
» python
Python 3.6.4 (default, Feb 22 2018, 09:26:37)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.__version__
'2.0.4'
>>>
/myvenv/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
Traceback (most recent call last):
File "/myvenv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/myvenv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/myvenv/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 168, in get_new_connection
connection = Database.connect(**conn_params)
File "/myvenv/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "postgres" to address: No address associated with hostname
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 24, in <module>
execute_from_command_line(sys.argv)
File "/myvenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/myvenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/myvenv/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/myvenv/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/myvenv/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 92, in handle
loader.check_consistent_history(connection)
File "/myvenv/lib/python3.6/site-packages/django/db/migrations/loader.py", line 275, in check_consistent_history
applied = recorder.applied_migrations()
File "/myvenv/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 61, in applied_migrations
if self.has_table():
File "/myvenv/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 44, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "/myvenv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
return self._cursor()
File "/myvenv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor
self.ensure_connection()
File "/myvenv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/myvenv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/myvenv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/myvenv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/myvenv/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 168, in get_new_connection
connection = Database.connect(**conn_params)
File "/myvenv/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not translate host name "postgres" to address: No address associated with hostname
从容器运行迁移工作:
root@7dc4a3001a46:/code# python manage.py makemigrations
Migrations for 'something':
proj/something/migrations/0001_initial.py
- Create model Something
现在我必须将这个迁移从容器中移植到我的代码库中?这有多麻烦!?我也可以在我的主机上运行db,或者允许从dev主机连接到容器化数据库。
我仍然不清楚为什么我需要有一个数据库连接来准备迁移。
答案 0 :(得分:2)
makemigrations
命令正在尝试访问迁移的check the consistency数据库。
Ticket 26930建议您可以通过更改设置来使用dummy
后端来避免检查。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.dummy',
}
}