Skip to main content
 首页 » 编程设计

python之在 Heroku 上部署 Python(2.7)/Django(1.5) 应用程序时出现数据库完整性错误

2025年05月04日7shihaiming

最近我正在通过开发一个 reddit 克隆来学习 Python(2.7)/Django(1.5)。克隆几乎完成并且在我的本地环境 (db = sqlite3) 中完美运行。但是当我尝试在 Heroku (db = postgres) 上托管相同的东西时,事情就出错了。

具体来说,网络应用程序仍在加载,但登录、注销 和评论 部分会抛出错误(我使用的是 django -registrations-redux、django-comments 和 south)。

由于该应用已上线,您可以直接转到此处查看错误跟踪:

我的所有文件,包括 requirements.txt 等,都可以在这里找到:https://github.com/mhb11/unconnectedredditapp

让我感到困惑的是,这个东西在本地继续完美运行,但在 Heroku 上却不行。我假设我可能捏造了南方移民。但是,我整天都在删除我的 Heroku 应用程序并设置新的应用程序。每次我在新应用程序上运行 syncdb 命令时,在 Django-auth 系统安装和表创建后设置 super 用户凭据后,我都会收到以下错误:

DatabaseError: relation "links_userprofile" does not exist 
LINE 1: ...rofile"."user_id", "links_userprofile"."bio" FROM "links_use... 

我用 heroku run python2.7 manage.py syncdb --migrate 跟进(即 syncdb 和 migrate 一起),这次迁移不会喷出错误。但无论如何,该应用程序仍然会出现我在开始时指出的数据库错误。

我想我应该在我的本地设置中卸载 South,并使用旧的 syncdb 来处理所有事情。

在这一点上,我的 heroku 部署可能会出错,所以我知道您可能无法给我一个精确的解决方案。但是请用你的所有想法来打动我——我愿意尝试一切,因为这件事现在已经困扰我 48 小时了。帮助!

注意:如果您最终 checkout 克隆 (https://salty-ridge-5419.herokuapp.com/),请原谅非英文文本标签。


执行 git push heroku master 后运行 syncdb 命令 的错误跟踪:

(unconnectedreddit)has@has-VirtualBox:~/Desktop/uncreddit$ heroku run python2.7 manage.py syncdb 
Running `python2.7 manage.py syncdb` attached to terminal... up, run.8792 
CHECKING_HEROKU! 
ON_HEROKU! 
Syncing... 
Creating tables ... 
Creating table auth_permission 
Creating table auth_group_permissions 
Creating table auth_group 
Creating table auth_user_groups 
Creating table auth_user_user_permissions 
Creating table auth_user 
Creating table django_content_type 
Creating table django_session 
Creating table django_site 
Creating table django_admin_log 
Creating table django_comments 
Creating table django_comment_flags 
Creating table south_migrationhistory 
 
You just installed Django's auth system, which means you don't have any superusers defined. 
Would you like to create one now? (yes/no): yes 
Username (leave blank to use 'u8919'): mhb 
Email address: bhs@bhs.com 
Password:  
Password (again):  
DatabaseError: relation "links_userprofile" does not exist 
LINE 1: ...rofile"."user_id", "links_userprofile"."bio" FROM "links_use... 

请您参考如下方法:

通过运行 heroku config:set ON_HEROKU=1 解决了这个问题。在我的 settings.py 文件中,我在本地使用了 sqlite3,但在 Heroku 上恢复为 postgres。因此,在运行 syncdb 之前将 ON_HEROKU 设置为 1 ,解决了我的问题。