Django项目部署Heroku后出现Programming Error,本地运行正常
问题描述
我已经按照之前帖子的建议完成了所有迁移操作,也更新了settings.py文件,但在Heroku部署的Django应用访问时出现500错误,控制台日志如下:
2022-10-13T22:36:21.289647+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute 2022-10-13T22:36:21.289648+00:00 app[web.1]: with self.db.wrap_database_errors: 2022-10-13T22:36:21.289648+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__ 2022-10-13T22:36:21.289648+00:00 app[web.1]: raise dj_exc_value.with_traceback(traceback) from exc_value 2022-10-13T22:36:21.289648+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute 2022-10-13T22:36:21.289648+00:00 app[web.1]: return self.cursor.execute(sql, params) 2022-10-13T22:36:21.289648+00:00 app[web.1]: django.db.utils.ProgrammingError: relation "freelanceapp_post" does not exist 2022-10-13T22:36:21.289648+00:00 app[web.1]: LINE 1: ...t"."slug", "freelanceapp_post"."category_id" FROM "freelance... 2022-10-13T22:36:21.289648+00:00 app[web.1]: ^ 2022-10-13T22:36:21.289649+00:00 app[web.1]: 2022-10-13T22:36:21.290265+00:00 app[web.1]: 10.1.23.15 - - [13/Oct/2022:22:36:21 +0000] "GET / HTTP/1.1" 500 196793 "https://dashboard.heroku.com/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36" 2022-10-13T22:36:21.289879+00:00 heroku[router]: at=info method=GET path="/" host=victorkiprono.herokuapp.com request_id=b9f45047-6c65-44e5-975d-b0f71a4c023b fwd="105.161.25.118" dyno=web.1 connect=0ms service=133ms status=500 bytes=197110 protocol=https
解决方案
错误提示relation "freelanceapp_post" does not exist说明数据库中未生成对应的表,按以下步骤排查修复:
检查迁移文件是否已提交至Git
本地执行python manage.py makemigrations,如果输出有新的迁移文件,说明之前的迁移文件未同步到Heroku。执行以下命令提交并推送:git add . git commit -m "添加缺失的迁移文件" git push heroku main重新在Heroku上执行迁移
登录Heroku CLI,运行迁移命令:heroku run python manage.py migrate如果之前迁移有残留问题,可先重置数据库(注意:此操作会清空所有数据):
heroku pg:reset DATABASE heroku run python manage.py migrate验证数据库配置
确认settings.py中的数据库配置正确读取Heroku的环境变量,示例配置:import dj_database_url DATABASES = { 'default': dj_database_url.config(default='postgres://localhost/your_local_db') }确保requirements.txt中已包含
dj-database-url依赖包。确认表是否生成
进入Heroku数据库控制台查看表列表:heroku pg:psql在控制台输入
\dt,检查是否存在freelanceapp_post表。
内容的提问来源于stack exchange,提问作者KipronoKirui
相关产品推荐
相关产品推荐

