[英]Flask: peewee.OperationalError: no such table:
I'm trying to run https://github.com/swifthorseman/flask-peewee-heroku-setup locally on win 10, using python 3.6. 我正在尝试使用python 3.6在win 10上本地运行https://github.com/swifthorseman/flask-peewee-heroku-setup 。 I'm not sure if this project is designed to be run locally, maybe its only designed for heroku.
我不确定这个项目是否设计为在本地运行,也许它只是为heroku设计的。
I have run the file teletubbies.py locally which created the db and table as you can see in the screenshot. 我在本地运行了文件teletubbies.py,它创建了db和table,如屏幕截图所示。 To run it locally (or try to) I added the lines:
要在本地运行(或尝试),我添加了以下行:
if __name__ == '__main__':
app.run(debug=True, use_reloader=True)
and I'm running it using 我正在使用它
python server.py
The entire server.py file as I have it: 我拥有整个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)
By stepping through the code I don't see an error until the line: 通过单步执行代码我直到该行才看到错误:
tellytubbies = retrieve_all()
Which then crashes giving the following Traceback: 然后崩溃给出以下Traceback:
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
The retrieve_all() function comes from the teletubbies.py file: retrieve_all()函数来自teletubbies.py文件:
def retrieve_all():
results = []
for tellytubby in TellyTubby.select().order_by(TellyTubby.name):
results.append(tellytubby)
return results
the teletubbies.py does work when I run it as 当我运行teletubbies.py时,teletubbies.py可以正常工作
python teletubbies.py
How can I get this working? 我怎样才能使这个工作?
Your flask server works fine for me, after I run python teletubbies.py
to initialize the sqlite database. 在运行
python teletubbies.py
初始化sqlite数据库之后,我的烧瓶服务器正常工作。 However, if I remove the tellytubbies.db
file that was created by running the script directly, the flask server fails with the same exception you quote. 但是,如果我删除通过直接运行脚本创建的
tellytubbies.db
文件,则烧瓶服务器将失败并引用相同的异常。
Something to consider here is that when you run tellytubbies.py
, the database file tellytubbies.db
will be created in whatever directory you run it from . 这里需要考虑的是,当你运行
tellytubbies.py
,数据库文件tellytubbies.db
将在你运行它的任何目录中创建。 If you then run server.py
from a different directory - for instance, if your IDE is running it - it won't find the other tellytubbies.db
, so a new one will be created; 然后,如果您从另一个目录运行
server.py
- 例如,如果您的IDE正在运行它 - 它将找不到其他tellytubbies.db
,因此将创建一个新的; but becase the tables were only created in your __main__
section in tellytubbies.py
, the tables won't be created. 但是因为表只是在
tellytubbies.py
的__main__
部分创建的,所以不会创建表。
So your flask server needs to do some of the initialization that teletubbies.py
is doing. 所以你的烧瓶服务器需要做一些
teletubbies.py
正在进行的初始化。 I replaced the if __name__ == '__main__':
section of your server.py
with the following and it it works. 我用以下内容替换了
server.py
的if __name__ == '__main__':
部分,它可以正常工作。 It will work whether you keep the same database file or remove it between runs to start from scratch. 无论您是保留相同的数据库文件还是在运行之间将其从头开始删除它都将起作用。 I've added a check to make sure the same rows don't get inserted repeatedly.
我添加了一个检查,以确保不会重复插入相同的行。
if __name__ == '__main__':
from teletubbies import add_tellytubbies, TellyTubby
db_proxy.connect()
db_proxy.create_tables([TellyTubby], safe=True)
if TellyTubby.select().count() == 0:
add_tellytubbies()
app.run(debug=True, use_reloader=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.