【问题标题】:Django connection cursor not showing any tables?Django连接光标不显示任何表?
【发布时间】:2018-06-15 06:37:20
【问题描述】:

我正在尝试在单独的问题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) 版本中,MigrationRecorderensure_schema 方法是

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() 设置了跟踪。

现在,当我进入调试器时,我看到 self.connection.cursor() 上的 introspection.table_names() 返回一个空列表:

(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 中我可以看到数据库有表:

为什么这些表没有被self.connection.cursor()“拾取”?

【问题讨论】:

    标签: python django postgresql


    【解决方案1】:

    事实证明,我必须将架构的所有者设置为DATABASES['USER'],在我们的例子中称为'lucyapp'。最后,我使用pg_dump--no-owner--role=lucyapp 做到了这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 2023-01-09
      相关资源
      最近更新 更多