Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relation does not exist Django Postgres

I recently changed the database from sqlite3 to Postgres. My Django apps are running perfectly, but when I go to the admin page and click on my predicts model, it says

ProgrammingError at /admin/Atlus/predicts/

relation "Atlus_predicts" does not exist LINE 1: SELECT COUNT(*) AS "__count" FROM "Atlus_predicts"

models.py

from django.db import models
from django import forms

class predicts(models.Model):

    firstname =models.CharField(max_length=15)
    info = models.TextField()

    def __str__(self):
        return self.firstname

Traceback

Environment:


Request Method: GET
Request URL: http://localhost:8000/admin/Atlus/predicts/

Django Version: 2.2.5
Python Version: 3.8.0
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'bootstrap4',
 'rest_framework',
 'corsheaders',
 'main',
 'accounts',
 'Atlus',
 'Boomerang']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/db/backends/utils.py" in _execute
  84.                 return self.cursor.execute(sql, params)

The above exception (relation "Atlus_predicts" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "Atlus_predicts"
                                          ^
) was the direct cause of the following exception:

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/contrib/admin/options.py" in wrapper
  606.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/contrib/admin/sites.py" in inner
  223.             return view(request, *args, **kwargs)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/utils/decorators.py" in _wrapper
  45.         return bound_method(*args, **kwargs)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/contrib/admin/options.py" in changelist_view
  1672.             cl = self.get_changelist_instance(request)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/contrib/admin/options.py" in get_changelist_instance
  731.         return ChangeList(

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/contrib/admin/views/main.py" in __init__
  82.         self.get_results(request)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/contrib/admin/views/main.py" in get_results
  210.         result_count = paginator.count

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/utils/functional.py" in __get__
  80.         res = instance.__dict__[self.name] = self.func(instance)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/core/paginator.py" in count
  91.             return c()

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/db/models/query.py" in count
  392.         return self.query.get_count(using=self.db)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/db/models/sql/query.py" in get_count
  504.         number = obj.get_aggregation(using, ['__count'])['__count']

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/db/models/sql/query.py" in get_aggregation
  489.         result = compiler.execute_sql(SINGLE)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/db/models/sql/compiler.py" in execute_sql
  1100.             cursor.execute(sql, params)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/db/backends/utils.py" in execute
  99.             return super().execute(sql, params)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/db/backends/utils.py" in execute
  67.         return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/db/backends/utils.py" in _execute_with_wrappers
  76.         return executor(sql, params, many, context)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/db/backends/utils.py" in _execute
  84.                 return self.cursor.execute(sql, params)

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/db/utils.py" in __exit__
  89.                 raise dj_exc_value.with_traceback(traceback) from exc_value

File "/home/vipul/anaconda3/envs/librus/lib/python3.8/site-packages/django/db/backends/utils.py" in _execute
  84.                 return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /admin/Atlus/predicts/
Exception Value: relation "Atlus_predicts" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "Atlus_predicts"
                                          ^

This answer does not solve my problem ---->> Relation does not exist - Django & Postgres

I only have one admin account and this is my local machine. Github link: https://github.com/dragonblood/Librus Thanks

like image 558
vipul petkar Avatar asked Sep 12 '25 10:09

vipul petkar


1 Answers

You have to run python3 manage.py migrate after you change the database. You also need psycopg2 installed in your virtualenv and have you django settings set like that:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'postgres',
    'USER': 'postgres',
    'PASSWORD': 'postgres',
    'HOST': '127.0.0.1',
    'PORT': '5432',
}

}

like image 166
Andrey Merzlyakov Avatar answered Sep 15 '25 00:09

Andrey Merzlyakov