简体   繁体   中英

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. I'm not sure if this project is designed to be run locally, maybe its only designed for heroku.

I have run the file teletubbies.py locally which created the db and table as you can see in the screenshot. 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:

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 (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:

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

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. 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.

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 . 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; but becase the tables were only created in your __main__ section in tellytubbies.py , the tables won't be created.

So your flask server needs to do some of the initialization that teletubbies.py is doing. I replaced the if __name__ == '__main__': section of your server.py with the following and it it works. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM