首页 > 解决方案 > heroku postgres psycopg2.errors.UndefinedTable

问题描述

有人对使用 heroku 提供的免费 postgres 版本部署 heroku django 应用程序有任何提示吗?如果我没有python manage.py migrate在 django 构建过程中执行,那么当我在本地测试时,回溯中的这个错误几乎看起来很相似。

对 django、heroku 和 postgress 来说也是新手,所以提示很有帮助。这些是我使用 heroku 管道进行部署的步骤:

git push heroku master
heroku ps:scale web=1
heroku open

这是当我尝试查看网页时通过 heroku 日志返回的跟踪:

2021-04-28T15:42:39.226298+00:00 heroku[web.1]: State changed from crashed to starting
2021-04-28T15:42:46.842959+00:00 heroku[web.1]: Starting process with command `gunicorn mysite.wsgi --log-file -`
2021-04-28T15:42:50.000000+00:00 app[api]: Build succeeded
2021-04-28T15:42:50.613040+00:00 app[web.1]: [2021-04-28 15:42:50 +0000] [4] [INFO] Starting gunicorn 20.1.0
2021-04-28T15:42:50.614019+00:00 app[web.1]: [2021-04-28 15:42:50 +0000] [4] [INFO] Listening at: http://0.0.0.0:38735 (4)
2021-04-28T15:42:50.614239+00:00 app[web.1]: [2021-04-28 15:42:50 +0000] [4] [INFO] Using worker: sync
2021-04-28T15:42:50.624501+00:00 app[web.1]: [2021-04-28 15:42:50 +0000] [9] [INFO] Booting worker with pid: 9
2021-04-28T15:42:50.648659+00:00 app[web.1]: [2021-04-28 15:42:50 +0000] [10] [INFO] Booting worker with pid: 10
2021-04-28T15:42:51.320006+00:00 heroku[web.1]: State changed from starting to up
2021-04-28T15:43:08.733160+00:00 app[web.1]: Internal Server Error: /
2021-04-28T15:43:08.733171+00:00 app[web.1]: Traceback (most recent call last):
2021-04-28T15:43:08.733172+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
2021-04-28T15:43:08.733173+00:00 app[web.1]: return self.cursor.execute(sql, params)
2021-04-28T15:43:08.733174+00:00 app[web.1]: psycopg2.errors.UndefinedTable: relation "blog_post" does not exist
2021-04-28T15:43:08.733174+00:00 app[web.1]: LINE 1: SELECT COUNT(*) AS "__count" FROM "blog_post" WHERE "blog_po...
2021-04-28T15:43:08.733175+00:00 app[web.1]: ^

UPDATE,正如第一条评论中漂亮地提到的那样,你做了教程吗?在将数据库部分 部署到 heroku 时,我错过了这一点heroku run python manage.py migrate。完成整个教程,确保它先在本地运行!

标签: pythondjangopostgresqlheroku

解决方案



很高兴你知道了

一个建议在 Procfile 的顶部添加这一行。

release: python manage.py migrate

这会做什么?
它将在每次推送时自动运行命令迁移。


推荐阅读