sqlalchemy/alembic · error · Exception
No context has been configured yet.
Error message
No context has been configured yet.
What it means
Raised as a bare Exception by EnvironmentContext.get_context() (environment.py:1046-1056) when self._migration_context is None — meaning EnvironmentContext.configure() has not yet run. Almost every useful EnvironmentContext method (get_context, get_bind, run_migrations, begin_transaction) funnels through get_context(), so calling any of them pre-configure raises this.
Solutions
- Ensure context.configure(connection=..., ...) runs before any context.get_bind()/get_context()/run_migrations() call.
- In env.py, structure online mode as: if context.is_offline_mode(): run_offline; else: connect -> context.configure(connection=conn, ...) -> context.run_migrations().
- If invoking migrations programmatically, call command.upgrade()/command.stamp() which configure the context for you.
Example fix
// before
def run_migrations_online():
bind = context.get_bind() # called before configure -> raises
context.configure(connection=bind)
// after
def run_migrations_online():
connectable = engine
with connectable.connect() as connection:
context.configure(connection=connection)
with context.begin_transaction():
context.run_migrations() Defensive patterns
Strategy: validation
Validate before calling
# Ensure configure() ran before touching context bindings.
def configured():
from alembic.runtime.environment import EnvironmentContext
return EnvironmentContext._migration_context is not None
if not configured():
raise RuntimeError('Call context.configure(...) before get_bind()/run_migrations()')
bind = context.get_bind() Type guard
def context_is_configured(context) -> bool:
return getattr(context, '_migration_context', None) is not None Prevention
- In env.py, always call context.configure(...) before get_bind()/run_migrations().
- Use the standard env.py scaffolding (online/offline branches) rather than ad-hoc calls.
- For programmatic use, prefer alembic.command.* which configures the context for you.
When it happens
Trigger: Calling context.get_bind() or context.get_context() inside env.py before the `context.configure(...)` call; referencing `context` in a script that bypassed the standard env.py entry point; running a migration function that was imported and invoked directly without configuring the environment.
Common situations: Custom test harnesses that import env.py functions but never call configure(); env.py edited so configure() is conditional and skipped on a code path; multi-phase scripts that call get_bind() too early.
Related errors
- Connection, url, or dialect_name is required.
- A plugin named is already registered
- Can't drop table in batch mode
- constraint cannot be produced; original constraint is not…
- Constraint must have a name
AI-assisted analysis of sqlalchemy/alembic@5551b5d35f (2026-08-11).
Data as JSON: /api/errors/5813f28553a2788f.
Report an issue: GitHub.
Appendix: source
Thrown at alembic/runtime/environment.py:1055
has more specific transactional needs can of course
manipulate the :class:`~sqlalchemy.engine.Connection`
directly to produce transactional state in "online"
mode.
"""
return self.get_context().begin_transaction()
def get_context(self) -> MigrationContext:
"""Return the current :class:`.MigrationContext` object.
If :meth:`.EnvironmentContext.configure` has not been
called yet, raises an exception.
"""
if self._migration_context is None:
raise Exception("No context has been configured yet.")
return self._migration_context
def get_bind(self) -> Connection:
"""Return the current 'bind'.
In "online" mode, this is the
:class:`sqlalchemy.engine.Connection` currently being used
to emit SQL to the database.
This function requires that a :class:`.MigrationContext`
has first been made available via :meth:`.configure`.
"""
return self.get_context().bind # type: ignore[return-value]
def get_impl(self) -> DefaultImpl:
return self.get_context().impl
View on GitHub (pinned to 5551b5d35f)