简体   繁体   中英

Relation does not exist error in Django after postgresql integration

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.

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, eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM