I've made changes to a model in a Django app and need to apply migrations to the database. I'm new to Docker, so this is giving me problems. I've been able to makemigrations
and migrate
, but migration files aren't being created, so I can't push my code to production because of the missing migration files. I think this is because I'm running makemigrations
while connected to the Docker container.
If I try to makemigrations
without being connected to a running container, I get the following error:
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
Since I'm able to successfully migrate while connected to a running container, is there a way I can get it to produce migration files for me, that I can add to git? Or do I need to figure out how to run makemigrations
from outside of Docker?
I'd look into using bind mounts to connect the migrations folder inside your container to the folder on your local machine. That way, when you run the makemigraions command in the container, the migration files should be reflected locally too