4

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

4
  • 2
    Looks like you did not migrate properly. Commented Dec 17, 2019 at 9:50
  • This is the article i followed to change the database. digitalocean.com/community/tutorials/… Commented Dec 17, 2019 at 10:02
  • If you ran manage.py migrate then try manage.py showmigrations. Check in the list whether there is a migration for the predicts model. Also check inside your migrations folder of the Altus app for the migration that creates the predicts table. Check that it's shown in the result of showmigrations. Commented Dec 17, 2019 at 11:28
  • related: stackoverflow.com/q/41652951 Commented Dec 2, 2022 at 16:52

5 Answers 5

2

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

}

Sign up to request clarification or add additional context in comments.

2 Comments

I have installed psycopg2 and did both "makemigrations and migrate"
Do you have Postgres running? If you are on a linux machine, try running systemctl start postgresql . Also optionally, run systemctl enable postgresql
1
 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

Comments

0

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.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.