-1

Doing a site upgrade for Django, now pushing it to the server when I try python manage.py makemigrations I get this error

(kpsga) sammy@kpsga:~/webapps/kpsga$ python manage.py makemigrations
Traceback (most recent call last):
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "django_site" does not exist
LINE 1: ..."django_site"."domain", "django_site"."name" FROM "django_si...
                                                            ^


The above exception was the direct cause of the following exception:
...

File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/sammy/webapps/kpsga/kpsga/urls.py", line 27, in <module>
    path('blog', include('blog.urls')),
...
File "/home/sammy/webapps/kpsga/blog/urls.py", line 2, in <module>
    from blog.views import LatestBlogEntries, blog_archive, blog_entry_by_id, blog_entry
File "/home/sammy/webapps/kpsga/blog/views.py", line 10, in <module>
    class LatestBlogEntries(Feed):
File "/home/sammy/webapps/kpsga/blog/views.py", line 11, in LatestBlogEntries
    current_site = Site.objects.get_current()
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/contrib/sites/models.py", line 58, in get_current
    return self._get_site_by_id(site_id)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/contrib/sites/models.py", line 30, in _get_site_by_id
    site = self.get(pk=site_id)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/models/query.py", line 425, in get
    num = len(clone)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/models/query.py", line 269, in __len__
    self._fetch_all()
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/models/query.py", line 1308, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1156, in execute_sql
    cursor.execute(sql, params)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "django_site" does not exist
LINE 1: ..."django_site"."domain", "django_site"."name" FROM "django_si...

though I've got these added to the settings file

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',


   
    #additional django
    'django.contrib.sites',
]

SITE_ID = 1

LatestClass

class LatestBlogEntries(Feed):
    current_site = Site.objects.get_current()
    title = current_site.name + " - Latest News"
    link = "/news/"
    description = "Latest news and updates from " + current_site.domain
    
    def items(self):
        return BlogEntry.objects.all()[:10]
    def item_title(self, item):
        return item.title
    def item_description(self, item):
        return item.description
Sam B.
  • 2,703
  • 8
  • 40
  • 78

2 Answers2

0

The ProgrammingError is a database error. It means that the table you are attemping to read is not ready, because you attemp to acces to it before it is created:

class LatestBlogEntries(Feed):
    current_site = Site.objects.get_current()  <---- this line

You shoud load the static current_site variable in an independent function using post_migrate signal. There is an example in the docs

In case you find this overkill, there are workarrounds like:

  1. Catch the error. This will allow you to migrate, and in the next restart of the application it will read the object flawlessly.
    from django.db import ProgrammingError
    class LatestBlogEntries(Feed):
        try:
            current_site = Site.objects.get_current()
        except ProgrammingError:
            pass
  1. Loading the variable in _init_ method if not defined.

This usually happens when this operation is modeled with the tables already created, and then the database is cleared to start from scratch.

Anr
  • 332
  • 3
  • 6
0

You shouldn't call Site.objects.get_current() in the LatestBlogEntries class. It requires a database lookup, which causes the relation does not exist error when you haven't run the initial migrations yet.

You can use methods for title and description. This way, the Site.objects.get_current() runs when the feed is accessed, not when Django starts.

class LatestBlogEntries(Feed):

    def title(self, obj):
        current_site = Site.objects.get_current()
        return current_site.name + " - Latest News"

    link = "/news/"

    def description(self, obj):
        current_site = Site.objects.get_current()
        return "Latest news and updates from " + current_site.domain
    
    def items(self):
        return BlogEntry.objects.all()[:10]
    def item_title(self, item):
        return item.title
    def item_description(self, item):
        return item.description
Alasdair
  • 298,606
  • 55
  • 578
  • 516