简体   繁体   English

postgresql集成后Django中不存在关系错误

[英]Relation does not exist error in Django after postgresql integration

I used sqlite3 during development, then changed my database settings to postgresql:我在开发过程中使用了 sqlite3,然后将我的数据库设置更改为 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.类别模型存在于 taksist 应用程序中。 If I take back my database settings to sqlite3, then application runs successfully.如果我将数据库设置恢复到 sqlite3,则应用程序将成功运行。

I cannot even run my server.我什至无法运行我的服务器。 Even if there is a problem with my model taksist/Category, my application should run.即使我的模型 taksist/Category 有问题,我的应用程序也应该运行。 Here strange thing is whatever command is written, I got this error.奇怪的是,无论写什么命令,我都收到了这个错误。 I cannot do makemigrations, migrate, runserver.我不能做 makemigrations、迁移、运行服务器。

What did I do to solve the issue:我做了什么来解决这个问题:

  • deleted migrations folder, and tried makemigrations删除迁移文件夹,并尝试 makemigrations
  • I created empty migrations folder with init .py inside, and tried makemigrations我在里面创建了init .py 的空迁移文件夹,并尝试了 makemigrations
  • python manage.py migrate --fake python manage.py migrate --fake

none of above worked.以上都没有奏效。

Here is taksist.category model, in case.这是 taksist.category 模型,以防万一。

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.甚至 runserver、makemigrations 和 migrate。 after change database settings to postgresql, i could not migrate.将数据库设置更改为 postgresql 后,我无法迁移。 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.这意味着 Django 将在导入该模块后立即尝试访问数据库。 If that module gets imported while Django is trying to initialize itself for migrate , you get an exception.如果在 Django 尝试为migrate初始化自身时导入了该模块,则会出现异常。

You can look at the traceback for your django.db.utils.ProgrammingError to figure out what the offending access is and fix it.您可以查看django.db.utils.ProgrammingError的回溯以找出违规访问是什么并修复它。 For instance, the above pattern would be better served by something like a get_default_blog() function.例如,使用get_default_blog()函数可以更好地为上述模式提供服务。

If I take back my database settings to sqlite3, then application runs successfully.如果我将数据库设置恢复到 sqlite3,则应用程序将成功运行。

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.这个错误还意味着,如果您重命名或删除当前正在运行的 SQLite 数据库,您也无法获得新的数据库,因为尝试migrate它会失败并出现类似的错误。 This is not just Postgres's "fault" here.这不仅仅是 Postgres 的“错误”。

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.如果是测试数据库执行py manage.py flush命令,然后再次运行py manage.py makemigrations ,然后运行py manage.py migrate命令。

That should resolve.那应该可以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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