关系在Django中不存在错误 [英] Relation does not exist error in Django

查看:98
本文介绍了关系在Django中不存在错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有关此问题有很多问题,我仔细研究了解决方案,但不幸的是,没有一个对我有用.

I know there are many questions about this problem, I looked through the solutions and unfortunately none of them worked for me.

我创建了一个名为"usermanagement"的新应用,并向该应用添加了模型.添加模型后,我在设置中将用户管理添加到INSTALLED_APPS中.然后我运行了python manage.py makemigrations和python manage.py migration.这一切都很好!我也尝试使用应用名称运行迁移.

I created a new app called "usermanagement", and added a model to the app. After adding the model I added usermanagement to INSTALLED_APPS in settings. Then I ran python manage.py makemigrations, and python manage.py migrate. This all worked fine! I also did try running the migrations with the app-name.

当我尝试使用以下方法将问题的新实例添加到Python-Django shell中的数据库时,问题就开始了:

The problems start when I try to add a new instance of the model to the database in the Python-Django shell, by using:

>>>a = ClubOfficial(name="randomName", email="randomemail@random.com")
>>>a.save()

我收到以下错误:

django.db.utils.ProgrammingError:关系"usermanagement_clubofficial"不存在第1行:插入"usermanagement_clubofficial"(名称",电子邮件")...

django.db.utils.ProgrammingError: relation "usermanagement_clubofficial" does not exist LINE 1: INSERT INTO "usermanagement_clubofficial" ("name", "email") ...

下面是模型代码:

class ClubOfficial(models.Model):

    name = models.CharField(max_length=254)
    email = models.EmailField(max_length=254)

如果有帮助,我使用postgresql,并尝试重新启动服务器.该程序中的其他应用程序也可以正常运行,只是usermanagemenet出现了此问题.

If it helps, I use postgresql, and have tried restarting the server. The other apps in the program also work perfectly fine, it is just usermanagemenet that has this problem.

有人知道会出什么问题吗?

Does anyone know what could be going wrong?

谢谢您的时间!

注意:我现在使用不同的名称创建了一个新应用,从用户管理中复制粘贴了内容,并且一切正常.我认为问题可能出在删除名为usermanagement的应用之前,还是在我再次创建它之前.也许这以某种方式弄乱了数据库.

Note: I created a new app now with a different name, copy-pasted things from usermanagement and everything worked fine. I think the problem might be that before there was an app named usermanagement which was deleted, before I created it again. Maybe that messed up the database somehow.

推荐答案

我遇到了这个问题.就我而言,我有一个以前使用过的django应用程序,尚未移至生产环境,因此我删除了我应用程序的migrations文件夹中的一切,然后使用django扩展名擦除了postgresql数据库并使用以下命令缓存了文件:

I ran into this. In my case I had a previously working django app, not yet moved to production, so I deleted everything in my app's migrations folder, then using django extensions I wiped the postgresql database and cached files with:

./manage.py clear_cache
./manage.py clean_pyc
./manage.py reset_schema
./manage.py reset_db
# then deleted all files (including __init__.py) from my app's migrations folder.

我验证了我的postgresql数据库没有表.然后我跑了:

I verified that my postgresql database had no tables. I then ran:

./manage.py makemigrations
./manage.py migrate

哪个给出了输出:

No changes detected
./manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  (about 11 more lines of output here which are similar)

值得注意的是,我的模型名称在迁移中无处可寻.我将表打印在我的postgresql数据库中,并且Widget和Section表都丢失了.运行应用程序给了我这个错误(我用"app"和"model"代替了他们的真实姓名):

It is notable that my model's names where nowhere in the migration. I printed the tables in my postgresql database and the Widget and Section tables were both missing. Running the app gave me this error (I substituted 'app' and 'model' for their real names):

ProgrammingError at /my_path
relation "app_model" does not exist
LINE 1: ..."."my_field", "app_model"."my_field" FROM "appname...

因此未在数据库中创建模型的表.我的应用程序的迁移文件夹也完全为空.解决的方法是向我的应用程序的迁移文件夹中添加一个空的 __ init __.py ,然后运行 makemigrations 即可在该文件夹中创建并保存迁移文件.然后可以使用 migrate 执行此操作.您可以通过运行 makemigrations 并在Migrations/文件夹中使用 __ init __.py 或不使用 __ init __.py 来自己进行测试.

So the tables for the models were not being created in the database. My app's migrations folder was also completely empty. The solution was to just add an empty __init__.py to my app's migrations folder, and after that running makemigrations was able to create and save the migration file in the folder. This could then be executed with migrate. You can test this for yourself by running makemigrations with and without the __init__.py in the migrations/ folder.

此解决方案不是我的,而是用户 Ljubitel 在另一篇文章中提供的解决方案,但它不是那里的已接受答案,但是已接受的答案对我不起作用,因此我在此处编写此解决方案是为了希望能帮助别人.

This solution was not mine but one given by user Ljubitel on another post but it was not the accepted answer there, but the accepted answer didn't work for me so I wrote this solution here to hopefully help others.

这篇关于关系在Django中不存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆