Relation does not exist Django Postgres

Question:

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

Asked By: vipul petkar

||

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',
}

}

Answered By: Andrey Merzlyakov
 replace this with database code in settings.py (use your db name,user,password)

 DATABASES = {
     'default': {
     'ENGINE': 'django.db.backends.postgresql',
     'NAME': 'mydjangodb',
     'USER': 'root',
     'PASSWORD': 'CSGOroot',
     'HOST': 'localhost'
      }
  }

install psycopg 2 database adapter
perform migrations

  python manage.py migrate
  python manage.py makemigrations (your app name)
  python manage.py migrate

this should do

try: delete previous migrated file in migrations folder(file name something like this 001_initial.py)
then perform migrations again

Answered By: shaswat kumar

Always run your makemigration first, than migrate.

Your DATABASE variable in settings.py needs to point you to your preferred database. Psycopg2 is also required, or psycopg2-binary if you cant install the first one.

See the docs for the DB setup. But, as already answered, check your DB settings in settings.py.

Answered By: dev.ink

For me, this happened when I created a relationship to another table but fail to create that object to provide in this table:

company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name='company', null=True)

For you to have this field created in the database, you must first create the company so that you can put that company as input for the company field in this model.

Providing the company ID like this in postman

enter image description here

Answered By: SanRaph

Another solution is to make sure the commands run on the correct schema. For example, django tenants uses different schemas.

Answered By: Daniel Butler
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.