Flask:peewee.OperationalError:没有这样的表:

时间:2018-01-15 03:26:54

标签: python flask peewee

enter image description here

我尝试使用python 3.6在win 10上本地运行https://github.com/swifthorseman/flask-peewee-heroku-setup。我不确定这个项目是否设计为在本地运行,也许它只是为heroku设计的。

我在本地运行了文件teletubbies.py,它创建了db和table,如屏幕截图所示。要在本地运行(或尝试),我添加了以下行:

if __name__ == '__main__':
    app.run(debug=True, use_reloader=True)

我正在使用

运行它
python server.py

我拥有整个server.py文件:

from flask import Flask, render_template, g
from tellytubbies import retrieve_all, db_proxy

app = Flask(__name__)

@app.before_request
def before_request():
    g.db = db_proxy
    g.db.connect()

@app.after_request
def after_request(response):
    g.db.close()
    return response

@app.route('/')
def index():
    tellytubbies = retrieve_all()
    return render_template("index.html", tellytubbies=tellytubbies)

if __name__ == '__main__':
    app.run(debug=True, use_reloader=True)

通过单步执行代码,我不会看到错误,直到该行:

tellytubbies = retrieve_all()

然后崩溃,给出以下追溯:

Traceback (most recent call last):
  File "....myfile\lib\site-packages\flask\app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "....myfile\lib\site-packages\flask\app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "....myfile\lib\site-packages\flask\app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "....myfile\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "....myfile\lib\site-packages\flask\app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "....myfile\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "....myfile\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "....myfile\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "....myfile\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "....myfile\lib\site-packages\flask\app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "...\server.py", line 18, in index
    tellytubbies = retrieve_all()
  File "...\tellytubbies.py", line 32, in retrieve_all
    for tellytubby in TellyTubby.select().order_by(TellyTubby.name):
  File "....myfile\lib\site-packages\peewee.py", line 3281, in __iter__
    return iter(self.execute())
  File "....myfile\lib\site-packages\peewee.py", line 3274, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "....myfile\lib\site-packages\peewee.py", line 2939, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "....myfile\lib\site-packages\peewee.py", line 3837, in execute_sql
    self.commit()
  File "....myfile\lib\site-packages\peewee.py", line 3656, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "....myfile\lib\site-packages\peewee.py", line 135, in reraise
    raise value.with_traceback(tb)
  File "....myfile\lib\site-packages\peewee.py", line 3830, in execute_sql
    cursor.execute(sql, params or ())
peewee.OperationalError: no such table: tellytubby

retrieve_all()函数来自teletubbies.py文件:

def retrieve_all():
    results = []
    for tellytubby in TellyTubby.select().order_by(TellyTubby.name):
        results.append(tellytubby)
    return results

当我将其作为

运行时,teletubbies.py可以正常工作
python teletubbies.py

我怎样才能使这个工作?

1 个答案:

答案 0 :(得分:3)

你的烧瓶服务器可以正常使用, 后我运行python teletubbies.py来初始化sqlite数据库。但是,如果我删除通过直接运行脚本创建的tellytubbies.db文件,则烧瓶服务器将失败并引用相同的异常。

这里需要考虑的是,当您运行tellytubbies.py时,数据库文件tellytubbies.db将在运行它的目录中创建。然后,如果您从另一个目录运行server.py - 例如,如果您的IDE正在运行它 - 它将找不到另一个tellytubbies.db,因此将创建一个新的__main__;但由于表格仅在tellytubbies.py的{​​{1}}部分中创建,因此不会创建表格。

所以你的烧瓶服务器需要做一些teletubbies.py正在进行的初始化。我用以下内容替换了您if __name__ == '__main__':的{​​{1}}部分,并且它有效。无论您是保留相同的数据库文件还是在运行之间将其从头开始删除它都将起作用。我添加了一个检查,以确保不会重复插入相同的行。

server.py