JM:I have a dream,期待和你一起去实现!
原文及译文
JM
网友:Chris B.

        从sqlite切换到本地开发数据库postgres后,我无法迁移我的应用程序。我试过的一些修复和方法还没有解决(例如:Django:关系“django_site”不存在)。...

推荐:想去Google查资料?(你懂的)想建站?想测试?想挂机?想折腾?想要高性价比的服务器?点我!!


Django:关系“django_site”不存在于应用程序与psql使用网站框架(Django: relation “django_site” does not exist in app with psql using sites framework)

从sqlite切换到本地开发数据库postgres后,我无法迁移我的应用程序。我试过的一些修复和方法还没有解决(例如:Django:关系“django_site”不存在)。

python:3.6.3

Django版本:1.11.9

psql(PostgreSQL):10.1

已安装的应用

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

在.env文件中:

SITE_ID=1

我曾尝试过的解决方案:

清除所有迁移和迁移文件并运行:

$ ./manage.py makemigrations

那么我已经尝试了从django.contrib开始的应用程序的顺序和手动迁移,例如:

$ ./manage.py迁移网站(第一次)

然后应用其他迁移。但无论我如何订购应用程序迁移不会更改错误或允许迁移完成。

我也试着用-fake-initial进行迁移。

它看起来像是在创建网站模型之前调用一个网站对象。

项目/ 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 /版本/ 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)

迁移追踪:

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

管理追踪:

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...
                                                         ^

谢谢


after switching from sqlite to postgres for local dev db, I am unable to migrate my app. Several fixes and approaches I've attempted have not resolved (ex: Django: relation "django_site" does not exist).

python: 3.6.3

Django Version: 1.11.9

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:

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:

$ ./manage.py migrate sites (first)

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.

it looks like it is calling a site object before creating site model.

project/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:

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

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


解决方案(由网友Sumeet Kumar提供)

如果您可以登录到管理员,并可以看到组网站,那么请尝试修改和保存网站对象。

尝试按以下方式组织安装的应用程序中的条目:

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

让我知道如果这项工作。


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 Apps 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.


推荐:想去Google查资料?(你懂的)想建站?想测试?想挂机?想折腾?想要高性价比的服务器?点我!!


关于站长

JMJavaMethod的缩写,苦逼码农一个,一直想有番作为,奈何人老力衰,只能四处膜拜大佬以获得动力。已经从单机、局域网、互联网、混到了移动互联网,未来希望能在AI世界里继续混下去。这辈子有个终极目标:财务自由,心灵自由。

近期公告

一个人苦逼开发多日,网站终于正式上线,求关注!!! 一个人苦逼多日,终于正式上线,求关注!!!

找他  
猜你喜欢
想建站?想测试?
想挂机?想折腾?
想去Google查资料?(你懂的)
想要高性价比的服务器?
搬瓦工VPS - 性价最高的美国便宜VPS主机
友情链接

冀ICP备17016304号 版权所有 © JavaMethod.com All Rights Reserved,Theme by 拼图