Request and Query Lifecycle
March 5, 2026 ยท View on GitHub
This page explains the normal lifecycle of an Edgy-backed request/query execution path.
If you are troubleshooting lifecycle issues, read this together with Connection Management and Troubleshooting.
What
The lifecycle is the path from:
- starting the application lifecycle,
- executing queries,
- parsing model instances,
- closing registry/database scopes cleanly.
Why
Understanding this flow helps you avoid:
- repeated connection setup overhead,
DatabaseNotConnectedWarning,- schema/database context mistakes in multi-tenant and multi-db setups.
When
You usually need this model when:
- integrating with ASGI lifecycle hooks,
- running queries from sync code with
run_sync, - debugging query behavior across schemas/databases.
How
ASGI lifecycle path
In ASGI-native applications, the registry lifecycle is attached to the app lifecycle.
{!> ../docs_src/connections/asgi.py !}
Query execution path
During request handling:
Model.querybuilds a QuerySet.- QuerySet methods clone/compose the query state.
- Execution happens on await (
all,get,create, etc.). - Rows are parsed back into model instances.
- Related loading and signal hooks run where applicable.
sequenceDiagram
participant App
participant Registry
participant QuerySet
participant DB as Database
participant Model
App->>Registry: enter lifecycle (connect)
App->>QuerySet: build query (Model.query...)
QuerySet->>DB: compile + execute SQL
DB-->>QuerySet: rows
QuerySet-->>Model: parse rows to instances
Model-->>App: return model objects
App->>Registry: exit lifecycle (disconnect)
Sync lifecycle path
In sync-first code, wrap execution with with_async_env and call run_sync inside that scope.
{!> ../docs_src/connections/contextmanager.py !}
Example
For database/schema-aware querying, use:
Model.query.using(database=..., schema=...)with_schema(...)/set_schema(...)scoped helpers
See Queries.