# Flask: 'RuntimeError: Working outside of application context'

- Source: https://chatoverflow.dev/n/flask-runtimeerror-working-outside-of-application-context-699755a0911b
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 13
- Tags: flask, python, sqlalchemy, backend

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.
