1

I used sqlite3 during development, then changed my database settings to postgresql:

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

Regardless of any command I write, it gives me the error:

django.db.utils.ProgrammingError: relation "taksist_category" does not exist
LINE 1: ...st_category"."id", "taksist_category"."name" FROM "taksist_c...

Category model exists inside taksist application. If I take back my database settings to sqlite3, then application runs successfully.

I cannot even run my server. Even if there is a problem with my model taksist/Category, my application should run. Here strange thing is whatever command is written, I got this error. I cannot do makemigrations, migrate, runserver.

What did I do to solve the issue:

  • deleted migrations folder, and tried makemigrations
  • I created empty migrations folder with init.py inside, and tried makemigrations
  • python manage.py migrate --fake

none of above worked.

Here is taksist.category model, in case.

class Category(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length = 20)

    def __str__(self):
        return self.name

Any help of yours is appreciated. thank you in advance.

3
  • checked my model name it is correct everywhere, check INSTALLED_APPS, there is my app name. as I said, if I change my database settings to sqlite3, then everything is working well. I can register new user, and fetch info from database. but after switching to postgreql then this error happens. if there is a typo in my code then it should not work with sqlite3 as well, but it is working with sqlite3 Commented Jul 30, 2021 at 11:52
  • 1
    have you run python manage.py migrate Commented Jul 30, 2021 at 11:58
  • whatever command I execute, I got this error. even runserver, makemigrations and migrate. after change database settings to postgresql, i could not migrate. i can do nothing. Commented Jul 30, 2021 at 12:00

2 Answers 2

7

The root cause for "relation does not exist" is that migrations have not been run.

However, via the comments:

whatever command I execute, I got this error. even runserver, makemigrations and migrate. after change database settings to postgresql, i could not migrate. i can do nothing.

That is likely to mean you've written your application in a way that it accesses the database during initialization time, e.g. something like

from my_app.models import Blog

default_blog = Blog.objects.get(pk=1)

def view(...):

This means Django will attempt to access the database as soon as that module is imported. If that module gets imported while Django is trying to initialize itself for migrate, you get an exception.

You can look at the traceback for your django.db.utils.ProgrammingError to figure out what the offending access is and fix it. For instance, the above pattern would be better served by something like a get_default_blog() function.

If I take back my database settings to sqlite3, then application runs successfully.

This bug also means that if you rename or delete your current, working SQLite database, you can't get a new one going either, since attempting to migrate it would fail with a similar error. This is not just Postgres's "fault" here.

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

5 Comments

wow, thank you for you help. unbelievable approach to solve the problem. when I create taxiprofile model, I used category_choice = [(x.id, x.name) for x in Category.objects.all()]. so as you said It tries to fetch from table that does not exist, because first migration is not done yet. so i modified the code as: category_choice = []. now it worked :)
That sounds like that field should be a ForeignKey to a Category instead, but glad I could help in any case!
it is already ForeignKey, like this: class TaxiProfile(models.Model): category_choice = [(x.id, x.name) for x in Category.objects.all()] category = models.ForeignKey(Category, on_delete = models.SET_NULL, null = True, choices=category_choice, blank=True)
Then you don't need to set choices at all :)
I mage choices list empty, just to make first migrations successfully. after migrated I took back category_choice = [(x.id, x.name) for x in Category.objects.all()]. after migration it works fine, thank you again :)
0

If it' a test DB do a py manage.py flush command, then run the py manage.py makemigrations again, then the py manage.py migrate command.

That should resolve.

3 Comments

same mistake after running flush: django.db.utils.ProgrammingError: relation "taksist_category" does not exist LINE 1: ...st_category"."id", "taksist_category"."name" FROM "taksist_c... ^
when did you get this error, during makemigrations, or during migrate
makemigrations, migrate, runserver. all. as AKX said, it was not postgresql error. it is logic error. that is in my code I query the model that does not exist yet.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.