gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:56:47 27 4
gpt4 key购买 nike

为本地开发数据库从 sqlite 切换到 postgres 后,我无法为我的应用程序运行迁移。

我尝试过的一些修复和方法都没有解决(例如:Django: relation "django_site" does not exist)。

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/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)

迁移回溯:

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
[X] 0001_initial
[X] 0002_alter_domain_unique

管理员回溯:

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

谢谢

最佳答案

你说 manage.py showmigrations sites 显示以下内容:

sites
[X] 0001_initial
[X] 0002_alter_domain_unique

这意味着 Django 认为它已经为 sites 应用程序执行了迁移(也许这是因为你使用了 --fake-initial)

您可以使用 --fake 将站点迁移标记为未应用,然后重新运行迁移:

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

关于python - Django:关系 "django_site"在使用站点框架的 psql 应用程序中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48346403/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com