Я пытаюсь отладить проблему в отдельном вопросе, Django "MigrationSchemaMissing: невозможно создать таблицу django_migrations (не выбрана схема для создания)" : при попытке python manage.py migrate
Я получаю ошибку no schema has been selected to create in
.
В моей версии Django (1.11.9) метод ensure_schema
для MigrationRecorder
равен
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)
(может показаться, что в https://github.com/django/django/blob/master/django/db/migrations/recorder.py этослегка переработан, но по сути такой же).Обратите внимание, что я установил трассировку в начале метода, используя import ipdb; ipdb.set_trace()
.
Теперь, когда я захожу в отладчик, я вижу, что introspection.table_names()
в self.connection.cursor()
возвращает пустой список:
(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())
[]
Таким образом, похоже, что база данных не содержит таблиц и, в частности, не содержит таблицу django_migrations
.
Как это может быть?Если я посмотрю на нашу настройку DATABASES
, кажется, что она настроена правильно:
(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}}}
и в pgAdmin я вижу, что в базе данных есть таблицы:
![enter image description here](https://i.stack.imgur.com/bS85h.jpg)
Почему эти таблицы не «подхватываются» self.connection.cursor()
?