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

时间:2013-05-23 04:13:46

标签: 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', 'john.doe@example.com')
    db.session.add(user)
    db.session.commit()

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

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

我是这个python的新手,并且在heroku上发布了postgres,并在此链接的教程的帮助下完成了这段代码

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

更新:Heroku Log

    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 rodddy57@gmail.com
2013-05-23T03:58:51.530386+00:00 heroku[api]: Release v15 created by rodddy57@gmail.com
2013-05-23T03:58:51.575804+00:00 heroku[api]: Deploy 5348563 by rodddy57@gmail.com
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 rodddy57@gmail.com
2013-05-23T04:02:01.534823+00:00 heroku[api]: Release v16 created by rodddy57@gmail.com
2013-05-23T04:02:01.653570+00:00 heroku[api]: Deploy 558ce74 by rodddy57@gmail.com
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 rodddy57@gmail.com
2013-05-23T04:06:07.832028+00:00 heroku[api]: Release v17 created by rodddy57@gmail.com
2013-05-23T04:06:07.875828+00:00 heroku[api]: Deploy 6c3a03d by rodddy57@gmail.com
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 rodddy57@gmail.com
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 rodddy57@gmail.com
2013-05-23T04:32:58.898965+00:00 heroku[api]: Release v18 created by rodddy57@gmail.com
2013-05-23T04:32:58.938874+00:00 heroku[api]: Deploy 6828199 by rodddy57@gmail.com
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

1 个答案:

答案 0 :(得分:2)

您需要先创建表格:

db.create_all()

教程中提到了它。您只需运行一次(每次修改模型的结构时)。