Django连接游标没有显示任何表?

时间:2018-06-11 22:20:40

标签: python django postgresql

我正在尝试在单独的问题Django "MigrationSchemaMissing: Unable to create the django_migrations table (no schema has been selected to create in)"中调试问题:当我尝试python manage.py migrate时,我收到错误no schema has been selected to create in

在我的Django版本(1.11.9)中,ensure_schema的{​​{1}}方法是

MigrationRecorder

(似乎在https://github.com/django/django/blob/master/django/db/migrations/recorder.py中,这是稍微重构的,但基本上是相同的)。请注意,我已使用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) 在方法的开头设置了跟踪。

现在,当我进入调试器时,我看到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

在pgAdmin中我可以看到数据库有表:

enter image description here

为什么这些表格没有被(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}}} “接听”?

1 个答案:

答案 0 :(得分:0)

事实证明,我必须将架构的所有者设置为DATABASES['USER'],在我们的示例中称为'lucyapp'。最后,我使用pg_dump--no-owner--role=lucyapp进行了此操作。