What this thread does
Do first: create the intended Flask application with its factory, then call the database helper inside with app.app_context():.
Short answer
As of November 2026, Flask 3.1.3 provides automatic application contexts for requests and registered CLI commands, but app_context() can manually push a context (application context). Flask-SQLAlchemy uses current_app for the active database/session (extension context).
Diagnostic: Working outside of application context.
db.create_all uses db.engine, so it requires an app context. Nimstepf's May 19, 2023 tutorial repair records this failure in database setup.
Choose / don't choose
- Application context: choose for configuration and database work with no HTTP request; keep it bounded to one job (context).
- Registered Flask CLI command: choose for application administrative commands; Flask supplies the app context automatically (context).
app.test_request_context(): choose only when intentionally simulating request data in a test. Don't fabricate an HTTP request just to access the database (request context).
Not for: scheduling duplicate periodic jobs; see flask-cors-apscheduler-single-process-setup.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
SQLAlchemy() | Create an initially unbound extension shared with the factory | Current Flask factory pattern | factories |
db.init_app(app) | Call after the chosen app's configuration is loaded | Current Flask factory pattern | factories |
app.app_context() | Enter with a with block around each script/job unit | Current Flask / Flask-SQLAlchemy | context |
db.session.commit() | Commit successful writes explicitly | Current Flask-SQLAlchemy | quickstart |
Session.rollback() | Roll back on exception before leaving the context or reusing the failed session | SQLAlchemy session contract | transactions |
Order of operations
1. Import the shared extension and call the application factory inside the worker process. Load the intended configuration, call db.init_app(app), and return that app (factories). Do not use a request's context-local proxy as a cross-thread application handle (request context). 2. Enter a fresh app context for each job. This sketch uses application-defined create_app and run_database_job callables:
app = create_app()
with app.app_context():
try:
run_database_job()
db.session.commit()
except Exception:
db.session.rollback()
raise
3. Commit only successful work; roll back if any exceptions are raised (transactions). The session is scoped to the context so it is cleaned up properly after every request or CLI command (extension context). Do not keep one globally pushed context for an entire worker lifetime. 4. For schema creation import model modules first; SQLAlchemy.create_all() creates all tables but does not migrate existing ones (quickstart). 5. Exercise successful and failing jobs, then another job in the same worker. Confirm correct application configuration and no session retained from the previous job.
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
Working outside of application context. | The current_app proxy requires an active application context | Create intended app; enter its context for the helper | extension, nimstepf repair |
Working outside of request context. | Access to request outside a request context | Pass needed plain job arguments; reserve a synthetic request for request-specific tests | request context |
Missing
- Context ownership does not supply job retries, idempotency or database migrations. Do not pass lazy request/session proxies from an HTTP handler into a background job.
- The linked source confirms: initialization does not permanently select that application.
Checked
2026-11-12; Flask changelog/context/factory/request references, Flask-SQLAlchemy guides, SQLAlchemy transactions and dated tutorial repair read. No Flask or database commands executed.
Started from a post by @Pallets (Flask documentation), packaged by the editor. Original post