0

I’ve been trying to add a sitemap to my django website lately. While it looked ok on the local server, when it went live it returned the following error:

relation "django_site" does not exist LINE 1: ..."django_site"."domain", "django_site"."name" FROM "django_si...

I’ve been trying many things, notably the migration tricks I saw elsewhere but it never worked (even though I might have done it wrong; I'm new to this and not much at ease). I also get the same error when I try to load the admin page.

For info, I’m using Python 3.7.5 and Django version is 2.2.5.

This is how my settings file looks like:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.sitemaps',
'myapp',]

SITE_ID = 1

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',]

And here is some more info about the errors:

Traceback:

File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/utils.py" in _execute
  84.                 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 "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/sitemaps/views.py" in inner
  16.         response = func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/sitemaps/views.py" in sitemap
  53.     req_site = get_current_site(request)

File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/sites/shortcuts.py" in get_current_site
  13.         return Site.objects.get_current(request)

File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/sites/models.py" in get_current
  58.             return self._get_site_by_id(site_id)

File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/sites/models.py" in _get_site_by_id
  30.             site = self.get(pk=site_id)

File "/app/.heroku/python/lib/python3.7/site-packages/django/db/models/manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/app/.heroku/python/lib/python3.7/site-packages/django/db/models/query.py" in get
  402.         num = len(clone)

File "/app/.heroku/python/lib/python3.7/site-packages/django/db/models/query.py" in __len__
  256.         self._fetch_all()

File "/app/.heroku/python/lib/python3.7/site-packages/django/db/models/query.py" in _fetch_all
  1242.             self._result_cache = list(self._iterable_class(self))

File "/app/.heroku/python/lib/python3.7/site-packages/django/db/models/query.py" in __iter__
  55.         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)

File "/app/.heroku/python/lib/python3.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  1100.             cursor.execute(sql, params)

File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/utils.py" in execute
  99.             return super().execute(sql, params)

File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/utils.py" in execute
  67.         return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/utils.py" in _execute_with_wrappers
  76.         return executor(sql, params, many, context)

File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/utils.py" in _execute
  84.                 return self.cursor.execute(sql, params)

File "/app/.heroku/python/lib/python3.7/site-packages/django/db/utils.py" in __exit__
  89.                 raise dj_exc_value.with_traceback(traceback) from exc_value

File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/utils.py" in _execute
  84.                 return self.cursor.execute(sql, params)

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

Also, I retried performing a migration just in case (so deleted the pycache folder in my migrations folder, dropped the database and then ran a new migration using command line: python manage.py migrate). Everything seemed ok. Nevertheless I must add that my migrations folder is mostly empty. I just have the init.py file and the pycache folder which only contains a file named init.cpython-37.pyc. Is this normal?

Please let me know if you need further details.

Thanks in advance!

curiousIT
  • 133
  • 1
  • 10
  • Please provide the entire stack trace. – Shashank Singh Jan 14 '20 at 17:02
  • Hi @Shashank Singh, I've edited my initial post. I hope it will be useful. Thanks! – curiousIT Jan 14 '20 at 17:37
  • are you sure your migrations worked?? show the list of tables in your database... – Shashank Singh Jan 14 '20 at 17:48
  • The thing is I'm not really using the database. I do have a small db.sqlite3 file, but it was created automatically. I've never used it to store specific data. I tried to see its content with an sqlite viewer and it only returned this query with 0 results: SELECT * FROM 'auth_group' LIMIT 0,30 – curiousIT Jan 15 '20 at 10:20
  • Refer to the link and paste the result here. [Link](https://stackoverflow.com/a/83195/6758560) – Shashank Singh Jan 16 '20 at 03:13
  • OK, so this is what I got: django_migrations, sqlite_sequence, auth_group_permissions, auth_user_groups, auth_user_user_permissions, django_admin_log, django_content_type, auth_permission, auth_user, auth_group, django_session, django_site – curiousIT Jan 16 '20 at 16:40
  • how is the code deployed ??? in docker?? – Shashank Singh Jan 16 '20 at 19:14
  • No, I'm using git. Everytime I update my project, I apply these 3 command lines using the command prompt within my django virtual environment: git add --all, git commit and git push heroku master. – curiousIT Jan 16 '20 at 20:10
  • Hi, is there any other helpful information that I can add? Does anybody have an idea of what went wrong? – curiousIT Jan 20 '20 at 10:59
  • OK, so I managed to find the cause of the issue. I thought I already done it but obviously no. I came across a similar problem where one had to only comment out "'django.contrib.sites'," in the settings file. Doing this allowed both the sitemap and admin pages to get loaded. Nevertheless, wouldn't that cause other issues? – curiousIT Jan 23 '20 at 20:45

1 Answers1

2

OK, so I managed to find the cause of the issue. I thought I already done it but obviously no. I came across a similar problem where one had to only comment out "'django.contrib.sites'," in the settings file. Doing this allowed both the sitemap and admin pages to get loaded.

curiousIT
  • 133
  • 1
  • 10