Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

python - Django Migrating DB django.db.utils.ProgrammingError: relation "django_site" does not exist

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...