dan*_*ast 7 migration django docker
我在我的开发机器上工作。该代码在 docker 容器中的开发过程中进行了测试,因为:
docker-compose.ymlpostgres):我只想在我的开发机器中应用程序进行开发,而不是用于测试或运行应用程序。现在我正在尝试更新我的迁移:
» 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
Run Code Online (Sandbox Code Playgroud)
嗯,是的,当然,我的主机看不到postgres容器(只有正在运行的容器才能看到彼此,按照 docker compose 架构)
我可以连接到应用程序容器,在那里进行迁移,并将迁移检索到我的开发机器,但这似乎不是一个好的解决方案。
我的理解是迁移的计算基于:
我不明白为什么我需要一个数据库实例来进行迁移。
我可以在没有连接到数据库的情况下进行迁移吗?如何?
在下面添加完整的回溯。据我所知,vanillamakemigrations命令尝试连接到数据库。为什么会这样?
我在跑步:
» 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
Run Code Online (Sandbox Code Playgroud)
从容器运行迁移工作:
root@7dc4a3001a46:/code# python manage.py makemigrations
Migrations for 'something':
proj/something/migrations/0001_initial.py
- Create model Something
Run Code Online (Sandbox Code Playgroud)
现在我必须从容器中迁移到我的代码库?多麻烦啊!?我也可以在我的主机上运行数据库,或者允许从开发主机连接到容器化数据库。
我仍然不清楚为什么我需要一个数据库连接来准备迁移。
Ala*_*air 10
该makemigrations命令正在尝试访问数据库以检查迁移的一致性。
Ticket 26930建议您可以通过更改设置以使用dummy后端来避免检查。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.dummy',
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1521 次 |
| 最近记录: |