after switching from sqlite to postgres for local dev db, I am unable to run migrations for 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
./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:
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
)
You could use --fake
to mark the sites migrations as unapplied, then rerun migrate:
manage.py migrate sites zero --fake
manage.py migrate sites