python - 尝试在heroku上使用flask python将数据插入postgres时出错

标签 python postgresql heroku flask

这是我托管在heroku 上的Python 代码

import os
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
db = SQLAlchemy(app)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    email = db.Column(db.String(120), unique=True)

    def __init__(self, name, email):
        self.name = name
        self.email = email

    def __repr__(self):
        return '<Name %r>' % self.name


@app.route('/')
def hello():
    user = User('John Doe', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1973767177377d767c597c61787469757c377a7674" rel="noreferrer noopener nofollow">[email protected]</a>')
    db.session.add(user)
    db.session.commit()

当我在heroku上托管此代码时,它显示了一些内部服务器错误,当我检查heroku日志时,它显示了错误

ProgrammingError: (ProgrammingError) relation "user" does not exist

我对 Heroku 上的 python 和 postgres 很陌生,并在此链接上的教程的帮助下完成了这段代码

http://blog.y3xz.com/blog/2012/08/16/flask-and-postgresql-on-heroku/

更新:Heroku 日志

    2013-05-23T03:56:23.035100+00:00 app[web.1]: 2013-05-23 03:56:23 [2] [INFO] Starting gunicorn 0.17.4
2013-05-23T03:56:23.040293+00:00 app[web.1]: 2013-05-23 03:56:23 [7] [INFO] Booting worker with pid: 7
2013-05-23T03:56:23.402467+00:00 heroku[web.1]: State changed from starting to up
2013-05-23T03:56:18.874073+00:00 heroku[web.1]: State changed from crashed to starting
2013-05-23T03:56:39.017117+00:00 heroku[router]: at=info method=GET path=/ host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=1ms service=85ms status=404 bytes=238
2013-05-23T03:56:39.525303+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=0ms service=14ms status=404 bytes=238
2013-05-23T03:56:46.978140+00:00 heroku[router]: at=info method=GET path=/ host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=0ms service=2ms status=404 bytes=238
2013-05-23T03:56:47.384277+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=7ms service=8ms status=404 bytes=238
2013-05-23T03:57:06.556983+00:00 heroku[router]: at=info method=GET path=/ host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=2ms service=2ms status=404 bytes=238
2013-05-23T03:57:06.977769+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=4ms service=7ms status=404 bytes=238
2013-05-23T03:58:06.806000+00:00 heroku[slugc]: Slug compilation started
2013-05-23T03:58:51.504515+00:00 heroku[api]: Deploy 5348563 by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f4d505b5b5b460a087f58525e5653115c5052" rel="noreferrer noopener nofollow">[email protected]</a>
2013-05-23T03:58:51.530386+00:00 heroku[api]: Release v15 created by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="70021f14141409454730171d11191c5e131f1d" rel="noreferrer noopener nofollow">[email protected]</a>
2013-05-23T03:58:51.575804+00:00 heroku[api]: Deploy 5348563 by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5d7cac1c1c1dc9092e5c2c8c4ccc98bc6cac8" rel="noreferrer noopener nofollow">[email protected]</a>
2013-05-23T03:58:51.679327+00:00 heroku[web.1]: State changed from up to starting
2013-05-23T03:58:52.126498+00:00 heroku[slugc]: Slug compilation finished
2013-05-23T03:58:55.111308+00:00 heroku[web.1]: Starting process with command `gunicorn hello:app`
2013-05-23T03:58:57.328614+00:00 app[web.1]: 2013-05-23 03:58:57 [2] [INFO] Listening at: http://0.0.0.0:18877 (2)
2013-05-23T03:58:57.328816+00:00 app[web.1]: 2013-05-23 03:58:57 [2] [INFO] Using worker: sync
2013-05-23T03:58:57.327498+00:00 app[web.1]: 2013-05-23 03:58:57 [2] [INFO] Starting gunicorn 0.17.4
2013-05-23T03:58:57.333152+00:00 app[web.1]: 2013-05-23 03:58:57 [7] [INFO] Booting worker with pid: 7
2013-05-23T03:58:57.425061+00:00 heroku[web.1]: State changed from starting to up
2013-05-23T03:58:58.303480+00:00 app[web.1]: Hello
2013-05-23T03:59:14.984455+00:00 heroku[router]: at=info method=GET path=/ host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=0ms service=378ms status=404 bytes=238
2013-05-23T03:59:15.383483+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=2ms service=7ms status=404 bytes=238
2013-05-23T03:59:32.111379+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2013-05-23T03:59:32.961142+00:00 app[web.1]: 2013-05-23 03:59:32 [7] [INFO] Worker exiting (pid: 7)
2013-05-23T03:59:32.972065+00:00 app[web.1]: 2013-05-23 03:59:32 [2] [INFO] Handling signal: term
2013-05-23T03:59:32.980573+00:00 app[web.1]: 2013-05-23 03:59:32 [2] [INFO] Shutting down: Master
2013-05-23T03:59:34.234289+00:00 heroku[web.1]: Process exited with status 0
2013-05-23T03:59:45.869822+00:00 heroku[router]: at=info method=GET path=/ host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=1ms service=7ms status=404 bytes=238
2013-05-23T03:59:46.300009+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=1ms service=12ms status=404 bytes=238
2013-05-23T04:01:14.224039+00:00 heroku[slugc]: Slug compilation started
2013-05-23T04:02:01.509336+00:00 heroku[api]: Deploy 558ce74 by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="71031e15151508444631161c10181d5f121e1c" rel="noreferrer noopener nofollow">[email protected]</a>
2013-05-23T04:02:01.534823+00:00 heroku[api]: Release v16 created by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="087a676c6c6c713d3f486f65696164266b6765" rel="noreferrer noopener nofollow">[email protected]</a>
2013-05-23T04:02:01.653570+00:00 heroku[api]: Deploy 558ce74 by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9bba6adadadb0fcfe89aea4a8a0a5e7aaa6a4" rel="noreferrer noopener nofollow">[email protected]</a>
2013-05-23T04:02:02.088475+00:00 heroku[slugc]: Slug compilation finished
2013-05-23T04:02:02.239139+00:00 heroku[web.1]: State changed from up to starting
2013-05-23T04:02:04.231642+00:00 heroku[web.1]: Starting process with command `gunicorn hello:app`
2013-05-23T04:02:05.008155+00:00 app[web.1]: 2013-05-23 04:02:05 [2] [INFO] Listening at: http://0.0.0.0:25035 (2)
2013-05-23T04:02:05.007517+00:00 app[web.1]: 2013-05-23 04:02:05 [2] [INFO] Starting gunicorn 0.17.4
2013-05-23T04:02:05.011435+00:00 app[web.1]: 2013-05-23 04:02:05 [7] [INFO] Booting worker with pid: 7
2013-05-23T04:02:05.008265+00:00 app[web.1]: 2013-05-23 04:02:05 [2] [INFO] Using worker: sync
2013-05-23T04:02:05.393383+00:00 heroku[web.1]: State changed from starting to up
2013-05-23T04:02:05.867695+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2013-05-23T04:02:06.740906+00:00 app[web.1]: 2013-05-23 04:02:06 [2] [INFO] Handling signal: term
2013-05-23T04:02:06.749878+00:00 app[web.1]: 2013-05-23 04:02:06 [2] [INFO] Shutting down: Master
2013-05-23T04:02:06.740523+00:00 app[web.1]: 2013-05-23 04:02:06 [7] [INFO] Worker exiting (pid: 7)
2013-05-23T04:02:08.034370+00:00 heroku[web.1]: Process exited with status 0
2013-05-23T04:02:08.899418+00:00 heroku[router]: at=info method=GET path=/ host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=2ms service=4ms status=200 bytes=12
2013-05-23T04:02:18.449961+00:00 heroku[router]: at=info method=GET path=/ host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=6ms service=8ms status=200 bytes=12
2013-05-23T04:02:18.859110+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=1ms service=2ms status=404 bytes=238
20
13-05-23T04:02:09.407508+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=9ms service=36ms status=404 bytes=238
2013-05-23T04:06:07.812603+00:00 heroku[api]: Deploy 6c3a03d by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="186a777c7c7c612d2f587f75797174367b7775" rel="noreferrer noopener nofollow">[email protected]</a>
2013-05-23T04:06:07.832028+00:00 heroku[api]: Release v17 created by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="75071a1111110c4042351218141c195b161a18" rel="noreferrer noopener nofollow">[email protected]</a>
2013-05-23T04:06:07.875828+00:00 heroku[api]: Deploy 6c3a03d by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9cbd6ddddddc08c8ef9ded4d8d0d597dad6d4" rel="noreferrer noopener nofollow">[email protected]</a>
2013-05-23T04:06:08.324518+00:00 heroku[web.1]: State changed from up to starting
2013-05-23T04:06:09.884851+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2013-05-23T04:06:09.927762+00:00 heroku[web.1]: Starting process with command `gunicorn hello:app`
2013-05-23T04:06:10.169559+00:00 app[web.1]: 2013-05-23 04:06:10 [7] [INFO] Worker exiting (pid: 7)
2013-05-23T04:06:10.169859+00:00 app[web.1]: 2013-05-23 04:06:10 [2] [INFO] Handling signal: term
2013-05-23T04:06:10.176117+00:00 app[web.1]: 2013-05-23 04:06:10 [2] [INFO] Shutting down: Master
2013-05-23T04:06:10.763368+00:00 app[web.1]: 2013-05-23 04:06:10 [2] [INFO] Using worker: sync
2013-05-23T04:06:10.762676+00:00 app[web.1]: 2013-05-23 04:06:10 [2] [INFO] Starting gunicorn 0.17.4
2013-05-23T04:06:10.763273+00:00 app[web.1]: 2013-05-23 04:06:10 [2] [INFO] Listening at: http://0.0.0.0:33263 (2)
2013-05-23T04:06:10.766823+00:00 app[web.1]: 2013-05-23 04:06:10 [7] [INFO] Booting worker with pid: 7
2013-05-23T04:06:11.094232+00:00 heroku[web.1]: State changed from starting to up
2013-05-23T04:06:11.323674+00:00 heroku[web.1]: Process exited with status 0
2013-05-23T04:06:33.870116+00:00 heroku[router]: at=info method=GET path=/ host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=1ms service=178ms status=500 bytes=291
2013-05-23T04:06:34.300612+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=7ms service=3ms status=404 bytes=238
2013-05-23T04:06:39.029888+00:00 heroku[router]: at=info method=GET path=/ host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=1ms service=10ms status=500 bytes=291
2013-05-23T04:06:39.454465+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=2ms service=3ms status=404 bytes=238
2013-05-23T04:28:58.588850+00:00 heroku[api]: Starting process with command `python` by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="75071a1111110c4042351218141c195b161a18" rel="noreferrer noopener nofollow">[email protected]</a>
2013-05-23T04:29:02.045555+00:00 heroku[run.3895]: Awaiting client
2013-05-23T04:29:11.936975+00:00 heroku[run.3895]: Starting process with command `python`
2013-05-23T04:29:12.768923+00:00 heroku[run.3895]: State changed from starting to up
2013-05-23T04:30:12.580450+00:00 heroku[router]: at=info method=GET path=/ host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=2ms service=18ms status=200 bytes=12
2013-05-23T04:30:13.042360+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=9ms service=10ms status=404 bytes=238
2013-05-23T04:32:58.863745+00:00 heroku[api]: Deploy 6828199 by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="42302d2626263b777502252f232b2e6c212d2f" rel="noreferrer noopener nofollow">[email protected]</a>
2013-05-23T04:32:58.898965+00:00 heroku[api]: Release v18 created by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9deff2f9f9f9e4a8aaddfaf0fcf4f1b3fef2f0" rel="noreferrer noopener nofollow">[email protected]</a>
2013-05-23T04:32:58.938874+00:00 heroku[api]: Deploy 6828199 by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f2809d9696968bc7c5b2959f939b9edc919d9f" rel="noreferrer noopener nofollow">[email protected]</a>
2013-05-23T04:32:59.404078+00:00 heroku[web.1]: State changed from up to starting
2013-05-23T04:33:00.695790+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2013-05-23T04:33:00.933621+00:00 app[web.1]: 2013-05-23 04:33:00 [7] [INFO] Worker exiting (pid: 7)
2013-05-23T04:33:00.942116+00:00 app[web.1]: 2013-05-23 04:33:00 [2] [INFO] Handling signal: term
2013-05-23T04:33:00.949254+00:00 app[web.1]: 2013-05-23 04:33:00 [2] [INFO] Shutting down: Master
2013-05-23T04:33:01.140731+00:00 heroku[web.1]: Starting process with command `gunicorn hello:app`
2013-05-23T04:33:02.051225+00:00 app[web.1]: 2013-05-23 04:33:02 [2] [INFO] Using worker: sync
2013-05-23T04:33:02.050671+00:00 app[web.1]: 2013-05-23 04:33:02 [2] [INFO] Starting gunicorn 0.17.4
2013-05-23T04:33:02.051163+00:00 app[web.1]: 2013-05-23 04:33:02 [2] [INFO] Listening at: http://0.0.0.0:12731 (2)
2013-05-23T04:33:02.054757+00:00 app[web.1]: 2013-05-23 04:33:02 [7] [INFO] Booting worker with pid: 7
2013-05-23T04:33:02.215376+00:00 heroku[web.1]: Process exited with status 0
2013-05-23T04:33:02.391944+00:00 heroku[web.1]: State changed from starting to up
2013-05-23T04:33:09.030211+00:00 heroku[router]: at=info method=GET path=/ host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=6ms service=226ms status=500 bytes=291
2013-05-23T04:33:09.423291+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=4ms service=6ms status=404 bytes=238
2013-05-23T04:34:47.051731+00:00 heroku[router]: at=info method=GET path=/ host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=12ms service=21ms status=500 bytes=291
2013-05-23T04:34:47.481709+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=5ms service=7ms status=404 bytes=238
2013-05-23T04:35:48.326998+00:00 heroku[router]: at=info method=GET path=/ host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=5ms service=20ms status=500 bytes=291
2013-05-23T04:35:48.745971+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=1ms service=3ms status=404 bytes=238
2013-05-23T04:38:33.185389+00:00 heroku[router]: at=info method=GET path=/ host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=3ms service=17ms status=500 bytes=291
2013-05-23T04:38:33.600605+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=frozen-taiga-6747.herokuapp.com fwd="27.63.24.168" dyno=web.1 connect=0ms service=4ms status=404 bytes=238

最佳答案

您需要先创建表:

db.create_all()

教程里有提到。您只需运行一次(并且每次修改模型结构时)。

关于python - 尝试在heroku上使用flask python将数据插入postgres时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16705553/

相关文章:

python - 在 Google App Engine 中使用 ctypes 来使用二进制文件?

以错误的顺序创建 PostgreSQL 表

amazon-web-services - 在哪里可以找到等效的AWS上详细的Heroku日志?

python - 在 python 中使用 eval 动态创建类

python - 如何在 Python 中获取绝对文件路径

python - 如何计算神经网络模型中的精度、召回率和 F1 分数?

python - 将 `sqlachemy` 连接到 django 数据库

node.js - 如何使用 Node JS 设置 Babel 6 以在我的服务器端代码中使用 ES6?

javascript - bootstrap 下拉菜单在 Heroku 上不起作用

node.js - 在 Heroku 中 checkout GitHub 存储库