从sqlite切换到本地dev db的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 migrate sites
(第一次)
然后应用其他迁移。但无论我如何订购应用程序迁移都不会改变错误或允许迁移完成。
我还尝试使用--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的/分贝/后端/ 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:
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...
^
感谢
答案 0 :(得分:9)
您说manage.py showmigrations sites
显示以下内容:
sites
[X] 0001_initial
[X] 0002_alter_domain_unique
这意味着Django认为它已经为sites
app进行了迁移(也许这是因为你使用了--fake-initial
)
您可以使用--fake
将网站迁移标记为未应用,然后重新运行迁移:
manage.py migrate sites zero --fake
manage.py migrate sites
答案 1 :(得分:0)
如果您能够登录管理员并可以在其中看到网站组,请尝试修改并保存网站对象。
尝试按如下方式在已安装的应用中组织条目:
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sites',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
让我知道这是否有效。
答案 2 :(得分:0)
之前发生过这种情况,有些网站模型在迁移过程中是不存在的,我所做的只是将整个内容包装在try catch中,并且在初始网站迁移后似乎没有再发生异常。
date website+
0 2014-11-26 [A]
1 2015-12-20 [B, C]
2 2016-02-17 [D]
3 2016-02-23 [E, F, G]
0 A 2014-11-26
1 B 2015-12-20
C 2015-12-20
2 D 2016-02-17
3 E 2016-02-23
F 2016-02-23
G 2016-02-23
dtype: object