【问题标题】:Django/Postgres migration failing "django.db.utils.ProgrammingError: relation "django_site" does not exist"Django/Postgres 迁移失败“django.db.utils.ProgrammingError:关系“django_site”不存在”
【发布时间】:2015-05-20 17:51:37
【问题描述】:

迁移 Django 1.8.1 项目时遇到问题

Operations to perform:
  Synchronize unmigrated apps: raven_contrib_django, staticfiles, found_dash, messages, allauth, humanize
  Apply all migrations: account, found_auth, sessions, admin, sites, auth, found_assets, contenttypes
Synchronizing apps without migrations:
  Creating tables...
    Creating table allauth_socialapp
    Creating table allauth_socialaccount
    Creating table allauth_socialtoken
    Running deferred SQL...
Raven is not configured (logging is disabled). Please see the documentation for more information.
Traceback (most recent call last):
  File "src/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/found/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/found/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/found/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/found/env/local/lib/python2.7/site-packages/raven/contrib/django/management/__init__.py", line 41, in new_execute
    return original_func(self, *args, **kwargs)
  File "/found/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/found/env/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 179, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/found/env/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 317, in sync_apps
    cursor.execute(statement)
  File "/found/env/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/found/env/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/found/env/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "django_site" does not exist

它抱怨django_site 不存在。这是我的应用配置:

INSTALLED_APPS = (
    'django.contrib.sites',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',...

包括网站框架。此错误仅在使用 Postgres 数据库时发生,SQLite 数据库可以正常工作。

数据库配置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'found',
        'USER': 'found',
        'PASSWORD': 'xxx',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

完整配置here

./manage.py dbshell 进入数据库正常:

(env)deploy@w1:/found$ src/manage.py dbshell
Password for user found: 
psql (9.3.6)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.

found=> \dt
             List of relations
 Schema |       Name        | Type  | Owner 
--------+-------------------+-------+-------
 public | django_migrations | table | found
(1 row)

found=> 

运行./manage.py flush

(env)deploy@w1:/found$ src/manage.py flush
You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the 'found' database,
and return each table to an empty state.
Are you sure you want to do this?

    Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
  File "src/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/found/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/found/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/found/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/found/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/found/env/local/lib/python2.7/site-packages/django/core/management/commands/flush.py", line 84, in handle
    self.emit_post_migrate(verbosity, interactive, database)
  File "/found/env/local/lib/python2.7/site-packages/django/core/management/commands/flush.py", line 105, in emit_post_migrate
    emit_post_migrate_signal(set(all_models), verbosity, interactive, database)
  File "/found/env/local/lib/python2.7/site-packages/django/core/management/sql.py", line 280, in emit_post_migrate_signal
    using=db)
  File "/found/env/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 201, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/found/env/local/lib/python2.7/site-packages/django/contrib/sites/management.py", line 20, in create_default_site
    if not Site.objects.using(using).exists():
  File "/found/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 586, in exists
    return self.query.has_results(using=self.db)
  File "/found/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 479, in has_results
    return compiler.has_results()
  File "/found/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 808, in has_results
    return bool(self.execute_sql(SINGLE))
  File "/found/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 837, in execute_sql
    cursor.execute(sql, params)
  File "/found/env/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/found/env/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/found/env/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "django_site" does not exist
LINE 1: SELECT (1) AS "a" FROM "django_site" LIMIT 1

【问题讨论】:

  • 您的数据库设置是什么样的?你创建数据库了吗?使用 id/pass 访问它?
  • @taesu 添加到问题
  • 你试过./manage flush吗?
  • 是的,我认为问题在于迁移顺序搞砸了。您可以查看allauth.account 的源代码,它需要首先迁移sites,但不知何故我没有看到您的mange.py migrate 发生这种情况。这就是为什么我猜某处可能存在错误。
  • 是的,我得出了这个结论,这真的很奇怪,因为 sqlite 数据库从来没有这个问题,只有 postgres。

标签: django postgresql


【解决方案1】:

问题是 Django 内部的。在创建表时,它没有考虑您的应用对django_site 的依赖。

但是,您可以轻松避免为依赖django.contrib.sites 导致错误的应用创建迁移的问题。

通过以下方式解决此问题:

src/manage.py makemigrations allauth

(现有迁移将导致稍后创建表,并且还会在迁移的应用程序之间执行依赖关系检查。)

【讨论】:

    猜你喜欢
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 2021-03-17
    • 2021-11-03
    • 1970-01-01
    • 2017-11-18
    相关资源
    最近更新 更多