我的代码在本地工作,但是当我尝试在Heroku上运行它时它不起作用。我在heroku上添加了一个数据库,但它仍然无法工作。为什么会发生这种情况的任何线索?
import sys
import os
from flask import Flask, render_template
from flask.ext.sqlalchemy import SQLAlchemy
db_local = #it works, just not shown here
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', db_local)
app.secret_key = ' ' #it's a secret for a reason
db = SQLAlchemy(app)
from views import *
from model import *
db.create_all()
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)
我的一些heroku日志
2013-02-19T22:29:59+00:00 app[web.1]: File "app.py", line 23, in <module>
2013-02-19T22:29:59+00:00 app[web.1]: from views import *
2013-02-19T22:29:59+00:00 app[web.1]: File "/app/views.py", line 1, in <module>
2013-02-19T22:29:59+00:00 app[web.1]: from app import db
2013-02-19T22:29:59+00:00 app[web.1]: File "/app/app.py", line 25, in <module>
2013-02-19T22:29:59+00:00 app[web.1]: db.create_all()
2013-02-19T22:29:59+00:00 app[web.1]: self._execute_for_all_tables(app, bind, 'create_all')
2013-02-19T22:29:59+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask_sqlalchemy.py", line 822, in create_all
2013-02-19T22:29:59+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask_sqlalchemy.py", line 814, in _execute_for_all_tables
2013-02-19T22:29:59+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/schema.py", line 2564, in create_all
2013-02-19T22:29:59+00:00 app[web.1]: import utils
2013-02-19T22:29:59+00:00 app[web.1]: File "/app/utils.py", line 1, in <module>
2013-02-19T22:29:59+00:00 app[web.1]: Traceback (most recent call last):
2013-02-19T22:29:59+00:00 app[web.1]: conn = self.contextual_connect(close_with_result=False)
2013-02-19T22:29:59+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2490, in contextual_connect
2013-02-19T22:29:59+00:00 app[web.1]: self.pool.connect(),
2013-02-19T22:29:59+00:00 app[web.1]: tables=tables)
2013-02-19T22:29:59+00:00 app[web.1]: con = self._create_connection()
答案 0 :(得分:3)
我想您的配置变量中DATABASE_URL
的值不存在。您可以通过heroku config:get DATABASE_URL --app your-app
尝试heroku pg:info --app your-app
找出您安装的数据库。确保您的数据库也是DATABASE_URL(它也会显示在pg:info
的输出中)。如果不是,只需运行heroku pg:promote HEROKU_POSTGRESQL_<color> --app your-app
并再次尝试您的脚本。