I'm trying to debug an issue in a separate question, Django "MigrationSchemaMissing: Unable to create the django_migrations table (no schema has been selected to create in)": when I try to python manage.py migrate
, I get an error no schema has been selected to create in
.
In my version of Django (1.11.9), the ensure_schema
method of the MigrationRecorder
is
def ensure_schema(self):
"""
Ensures the table exists and has the correct schema.
"""
# If the table's there, that's fine - we've never changed its schema
# in the codebase.
import ipdb; ipdb.set_trace()
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
return
# Make the table
try:
with self.connection.schema_editor() as editor:
editor.create_model(self.Migration)
except DatabaseError as exc:
raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
(It would appear that in https://github.com/django/django/blob/master/django/db/migrations/recorder.py this is slightly refactored, but essentially the same). Note that I've set a trace at the beginning of the method using import ipdb; ipdb.set_trace()
.
Now, when I drop into the debugger, I see that introspection.table_names()
on the self.connection.cursor()
returns an empty list:
(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py migrate
> /Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/migrations/recorder.py(53)ensure_schema()
52 import ipdb; ipdb.set_trace()
---> 53 if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
54 return
ipdb> self.Migration._meta.db_table
'django_migrations'
ipdb> self.connection.introspection.table_names(self.connection.cursor())
[]
So it seems like the database does not contain any tables, and in particular, doesn't contain the django_migrations
table.
How can this be? If I look at our DATABASES
setting, it seems to be configured correctly:
(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py shell
Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.3.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from django.conf import settings
In [2]: settings.DATABASES
Out[2]:
{'default': {'NAME': 'lucy_prod',
'USER': 'lucyapp',
'PASSWORD': '<our_password>',
'HOST': 'localhost',
'PORT': '',
'CONN_MAX_AGE': 500,
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'ATOMIC_REQUESTS': False,
'AUTOCOMMIT': True,
'OPTIONS': {},
'TIME_ZONE': None,
'TEST': {'CHARSET': None, 'COLLATION': None, 'NAME': None, 'MIRROR': None}}}
and in pgAdmin I can see that the database has tables:
Why are these tables not getting 'picked up' by the self.connection.cursor()
?