Python – Flask:peewee. OperationalError: There is no such table:

Flask:peewee. OperationalError: There is no such table:… here is a solution to the problem.

Flask:peewee. OperationalError: There is no such table:

enter image description here

I’m trying to run https://github.com/swifthorseman/flask-peewee-heroku-setup is native to Win 10 and uses Python 3.6. I’m not sure if this project is designed to run locally, maybe it’s just designed for heroku.

I ran file teletubbies.py locally and it creates the database and tables, as you can see in the screenshot. To run it locally (or trying), I added the following line:

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

I’m using

to run it

python server.py

The entire server.py file I have:

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 the error before the following line:

tellytubbies = retrieve_all()

Then crashes and gives 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 teletubbies.py file:

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

It does work when I run teletubbies.py

python teletubbies.py

How can I get it to work?

Solution

After I ran python teletubbies.py initialized the sqlite database, your Flask server worked fine for me. However, if I delete the tellytubbies.db file created by running the script directly, the flask server fails with the same exception you referenced.

One thing 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 example, if your IDE is running it – it won’t find other tellytubbies.db, so a new one will be created; However, because these tables are created only in the __main__ section of the tellytubbies.py, they are not created.

So your flask server needs to do some initialization teletubbies.py is doing. I replaced the if __name__ == '__main__': part of server.py with the following, and it works fine. It will work whether you keep the same database file or drop it between runs to start from scratch. I added checks to make sure the same rows are not 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)

Related Problems and Solutions