简体   繁体   English

Django:关系“django_site”在使用站点框架的psql应用程序中不存在

[英]Django: relation “django_site” does not exist in app with psql using sites framework

after switching from sqlite to postgres for local dev db, I am unable to run migrations for my app. 从sqlite切换到本地dev db的postgres后,我无法为我的应用程序运行迁移。

Several fixes and approaches I've attempted have not resolved (ex: Django: relation "django_site" does not exist ). 我试过的一些修复和方法尚未解决(例如: Django:关系“django_site”不存在 )。

python: 3.6.3 python:3.6.3

Django Version: 1.11.9 Django版本:1.11.9

psql (PostgreSQL): 10.1 psql(PostgreSQL):10.1

installed apps: 安装的应用:

DJANGO_APPS = (
# Default Django apps:
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'django.contrib.humanize',
'django.contrib.admin',
)

THIRD_PARTY_APPS = (
'widget_tweaks',
'mptt',
'channels',
'honeypot',
'gunicorn',
'djangosecure',

# Allauth
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
)

LOCAL_APPS = (
'users.apps.UsersConfig', #because of a signal
'common',
'geo',
'community',
'objects',
)

INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

in .env file: 在.env文件中:

SITE_ID=1

solutions I have attempted: 我试过的解决方案:

Cleared all migrations and migration files and ran: 清除所有迁移和迁移文件并运行:

$ ./manage.py makemigrations

then I have attempted sequential and manual migrations of apps starting with django.contrib, such as: 然后我尝试了以django.contrib开头的应用程序的顺序和手动迁移,例如:

$ ./manage.py migrate sites (first) $ ./manage.py migrate sites (第一个)

then applying additional migrations. 然后应用其他迁移。 but regardless of how I order app migrations does not change err or allow migration to complete. 但无论我如何订购应用程序迁移都不会改变错误或允许迁移完成。

I have also tried migrations with --fake-initial . 我也尝试过使用--fake-initial迁移。

it looks like it is calling a site object before creating site model. 它看起来像是在创建站点模型之前调用站点对象。

project/utils/middleware.py: 项目/ utils的/ middleware.py:

class SiteMiddleware(object):
    def process_request(self, request):
        try:
            current_site = Site.objects.get(domain=request.get_host())
        except Site.DoesNotExist:
            current_site = Site.objects.get(id=settings.SITE_ID)

        request.current_site = current_site

        if current_site.domain in settings.HOST_MIDDLEWARE_URLCONF_MAP:
            request.urlconf = settings.HOST_MIDDLEWARE_URLCONF_MAP[str(current_site)]

.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/utils.py: .pyenv /版本/ 3.6.3 / lib中/ python3.6 /站点包/ Django的/ DB /后端/ utils.py:

class CursorWrapper(object):
    def __init__(self, cursor, db):
        self.cursor = cursor
        self.db = db

    WRAP_ERROR_ATTRS = frozenset(['fetchone', 'fetchmany', 'fetchall', 'nextset'])

    def __getattr__(self, attr):
        cursor_attr = getattr(self.cursor, attr)
        if attr in CursorWrapper.WRAP_ERROR_ATTRS:
            return self.db.wrap_database_errors(cursor_attr)
        else:
            return cursor_attr

    def __iter__(self):
        with self.db.wrap_database_errors:
            for item in self.cursor:
                yield item

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        try:
            self.close()
        except self.db.Database.Error:
            pass

    def callproc(self, procname, params=None):
        self.db.validate_no_broken_transaction()
        with self.db.wrap_database_errors:
            if params is None:
                return self.cursor.callproc(procname)
            else:
                return self.cursor.callproc(procname, params)

    def execute(self, sql, params=None):
        self.db.validate_no_broken_transaction()
        with self.db.wrap_database_errors:
            if params is None:
                return self.cursor.execute(sql)
            else:
                return self.cursor.execute(sql, params)

    def executemany(self, sql, param_list):
        self.db.validate_no_broken_transaction()
        with self.db.wrap_database_errors:
            return self.cursor.executemany(sql, param_list)

migration traceback: 迁移追踪:

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 227, in handle
    self.verbosity, self.interactive, connection.alias, apps=post_migrate_apps, plan=plan,
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/management/sql.py", line 53, in emit_post_migrate_signal
    **kwargs
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 193, in send
    for receiver in self._live_receivers(sender)
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 193, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/contrib/sites/management.py", line 20, in create_default_site
    if not Site.objects.using(using).exists():
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/models/query.py", line 670, in exists
    return self.query.has_results(using=self.db)
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/models/sql/query.py", line 517, in has_results
    return compiler.has_results()
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 858, in has_results
    return bool(self.execute_sql(SINGLE))
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 899, in execute_sql
    raise original_exception
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 889, in execute_sql
    cursor.execute(sql, params)
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/.pyenv/versions/3.6.3/lib/python3.6/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

./manage.py showmigrations sites: ./manage.py showmigrations sites:

sites
 [X] 0001_initial
 [X] 0002_alter_domain_unique

admin traceback: 管理员追溯:

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
64.                 return self.cursor.execute(sql, params)

The above exception (relation "django_site" does not exist
LINE 1: ..."django_site"."domain", "django_site"."name" FROM "django_si...
                                                         ^
) was the direct cause of the following exception:

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
41.             response = get_response(request)

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/handlers/base.py" in _legacy_get_response
244.             response = middleware_method(request)

File "/Users/.../project/utils/middleware.py" in process_request
47.             current_site = Site.objects.get(domain=request.get_host())

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method
85.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/models/query.py" in get
374.         num = len(clone)

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/models/query.py" in __len__
232.         self._fetch_all()

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/models/query.py" in _fetch_all
1118.             self._result_cache = list(self._iterable_class(self))

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/models/query.py" in __iter__
53.         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
899.             raise original_exception

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
889.             cursor.execute(sql, params)

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
79.             return super(CursorDebugWrapper, self).execute(sql, params)

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
64.                 return self.cursor.execute(sql, params)

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/utils.py" in __exit__
94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/utils/six.py" in reraise
685.             raise value.with_traceback(tb)

File "/Users/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
64.                 return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /admin
Exception Value: relation "django_site" does not exist
LINE 1: ..."django_site"."domain", "django_site"."name" FROM "django_si...
                                                         ^

thanks 谢谢

You say that manage.py showmigrations sites shows the following: 您说manage.py showmigrations sites显示以下内容:

sites
[X] 0001_initial
[X] 0002_alter_domain_unique

That means that Django thinks it has already carried out the migrations for the sites app (perhaps this is because you used --fake-initial ) 这意味着Django认为它已经为sites app进行了迁移(也许这是因为你使用了--fake-initial

You could use --fake to mark the sites migrations as unapplied, then rerun migrate: 您可以使用--fake将站点迁移标记为未应用,然后重新运行迁移:

manage.py migrate sites zero --fake
manage.py migrate sites

If you are able to login to admin and can see group Sites there then please try modifying and saving the site object. 如果您能够登录管理员并可以在那里看到组网站 ,请尝试修改并保存站点对象。

Try organizing the entries in Installed App s as follows : 尝试按如下方式组织已安装应用程序中的条目:

'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sites',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

let me know if this work. 如果这项工作让我知道。

This happened to me before, some how the Site model does not exist during migration, all I did is wrap the whole thing in a try catch and the exception does not seem to happened again after the initial Site migration. 之前发生过这种情况,有些网站模型在迁移过程中是不存在的,我所做的只是将整个事情包装在try catch中,并且在初始网站迁移后似乎没有再发生异常。

class SiteMiddleware(object):
    def process_request(self, request):
        try:
            try:
                current_site = Site.objects.get(domain=request.get_host())
            except Site.DoesNotExist:
                current_site = Site.objects.get(id=settings.SITE_ID)

            request.current_site = current_site

            if current_site.domain in settings.HOST_MIDDLEWARE_URLCONF_MAP:
                request.urlconf = settings.HOST_MIDDLEWARE_URLCONF_MAP[str(current_site)]
        except Exception as ex:
            print('Error: %s' % ex)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM