gpt4 book ai didi

python - Django Heroku Postgres - 编程错误 : relation does not exist

转载 作者:行者123 更新时间:2023-11-29 13:54:39 24 4
gpt4 key购买 nike

我正在使用 Django 1.7、PostgresApp (PostgreSQL 9.4)、Python 2.7。

我已按照本教程设置和创建应用程序:https://docs.djangoproject.com/en/1.9/intro/tutorial02/

在 Python shell 中,对于我的数据库模式,我不断收到类似 "ProgrammingError: relation "app_table"does not exist"的错误。

所以我使用了教程中的类:

class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')


class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

然后在 shell 中我得到了(正确的值):

>>> Question.objects.all()
[]
>>> Choice.objects.all()
[]

然后,将名称 Choice 修改为 MyChoice 会得到:

>>> Question.objects.all()
[]
>>> MyChoice.objects.all()
...
ProgrammingError: relation "app_mychoice" does not exist.

为什么会这样?我尝试了其他帖子中建议的答案

./manage.py makemigrations; 
./manage.py migrate auth;
./manage.py migrate and
python manage.py syncdb

但没有任何效果。

最佳答案

您好,我在将现有应用迁移到 1.11 并对模型进行一些小改动时遇到了同样的问题。我找到的唯一解决方案是......

  1. 清除应用迁移目录中的所有文件,只留下init.py 文件

  2. 确保admin.py文件为空

  3. 运行 manage.py makemigrations

  4. 运行 manage.py sqlmigrate <app_label> 0001

  5. 复制sql输出

  6. 使用 pgAdminIII 选择“执行任意 SQL 查询”

  7. 在pgAdminIII中粘贴并执行SQL语句

这是我能找到的唯一解决方案,确实有点乱七八糟,但确实有效。希望它有所帮助。我想这也可以通过 psql 终端工作,但我使用了 pgAdmin

关于python - Django Heroku Postgres - 编程错误 : relation does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34656438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com