sqlalchemy/alembic · error · Exception

No context has been configured yet.

Error message

No context has been configured yet.

What it means

Raised by EnvironmentContext.get_context() when self._migration_context is None. The migration context is created as a side effect of EnvironmentContext.configure(); until configure() runs, no context exists. Methods like get_context(), get_bind(), get_impl(), and run_migrations() all route through get_context() and therefore require configure() to have been called first.

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 44fb345033)

Solutions

  1. Ensure EnvironmentContext.configure(...) (typically context.configure(...)) is called before any op.get_context()/op.get_bind()/run_migrations() usage.
  2. Verify the online and offline branches in env.py both reach a configure() call.
  3. Confirm the alembic command being run actually loads env.py and reaches the configure block.

Example fix

// before
bind = op.get_bind()  # raises: no context configured yet
context.configure(...)

// after
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
    run_migrations()
# op.get_bind() now works inside migration scripts invoked via run_migrations()
Defensive patterns

Strategy: validation

Validate before calling

def context_is_configured(env_context) -> bool:
    return getattr(env_context, '_migration_context', None) is not None

Try / catch

try:
    ctx = env.get_context()
except Exception as e:
    if 'No context has been configured' in str(e):
        env.configure(...)  # then retry
        ctx = env.get_context()
    else:
        raise

Prevention

When it happens

Trigger: Calling op.get_context() / op.get_bind() / env.get_context() before env.configure(...) has executed; an env.py that uses op.* before run_migrations(); a custom script that instantiates EnvironmentContext but forgets to call configure().

Common situations: Mis-ordered env.py; calling alembic operations at import time of env.py before the context is wired; running a migration command that bypasses configure (e.g. custom main()); the online vs offline setup branches both skipped.

Related errors


AI-assisted analysis of sqlalchemy/alembic@44fb345033 (2026-08-04). Data as JSON: /data/errors/5813f28553a2788f.json. Report an issue: GitHub.