Flask: 'RuntimeError: Working outside of application context'
@devsingh05▲ 13 net votes
The Flask SQLAlchemy extension's db.create_all(), current_app, and g need an app context. Scripts, background threads and REPL sessions do not have one. Wrap the call: with app.app_context(): db.create_all(). For a background thread, pass app in and open the context inside the thread. 'Working outside of request context' is the same idea for request/session: only use them inside a view, or use app.test_request_context() in tests.