{"id":"5813f28553a2788f","repo":"sqlalchemy/alembic","slug":"no-context-has-been-configured-yet","errorCode":null,"errorMessage":"No context has been configured yet.","messagePattern":"No context has been configured yet\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"alembic/runtime/environment.py","lineNumber":1055,"sourceCode":"        has more specific transactional needs can of course\n        manipulate the :class:`~sqlalchemy.engine.Connection`\n        directly to produce transactional state in \"online\"\n        mode.\n\n        \"\"\"\n\n        return self.get_context().begin_transaction()\n\n    def get_context(self) -> MigrationContext:\n        \"\"\"Return the current :class:`.MigrationContext` object.\n\n        If :meth:`.EnvironmentContext.configure` has not been\n        called yet, raises an exception.\n\n        \"\"\"\n\n        if self._migration_context is None:\n            raise Exception(\"No context has been configured yet.\")\n        return self._migration_context\n\n    def get_bind(self) -> Connection:\n        \"\"\"Return the current 'bind'.\n\n        In \"online\" mode, this is the\n        :class:`sqlalchemy.engine.Connection` currently being used\n        to emit SQL to the database.\n\n        This function requires that a :class:`.MigrationContext`\n        has first been made available via :meth:`.configure`.\n\n        \"\"\"\n        return self.get_context().bind  # type: ignore[return-value]\n\n    def get_impl(self) -> DefaultImpl:\n        return self.get_context().impl\n","sourceCodeStart":1037,"sourceCodeEnd":1073,"githubUrl":"https://github.com/sqlalchemy/alembic/blob/44fb3450330204b222ff05135e1fbbbdb28c44db/alembic/runtime/environment.py#L1037-L1073","documentation":"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.","triggerScenarios":"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().","commonSituations":"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.","solutions":["Ensure EnvironmentContext.configure(...) (typically context.configure(...)) is called before any op.get_context()/op.get_bind()/run_migrations() usage.","Verify the online and offline branches in env.py both reach a configure() call.","Confirm the alembic command being run actually loads env.py and reaches the configure block."],"exampleFix":"// before\nbind = op.get_bind()  # raises: no context configured yet\ncontext.configure(...)\n\n// after\ncontext.configure(connection=connection, target_metadata=target_metadata)\nwith context.begin_transaction():\n    run_migrations()\n# op.get_bind() now works inside migration scripts invoked via run_migrations()","handlingStrategy":"validation","validationCode":"def context_is_configured(env_context) -> bool:\n    return getattr(env_context, '_migration_context', None) is not None","typeGuard":null,"tryCatchPattern":"try:\n    ctx = env.get_context()\nexcept Exception as e:\n    if 'No context has been configured' in str(e):\n        env.configure(...)  # then retry\n        ctx = env.get_context()\n    else:\n        raise","preventionTips":["Call context.configure(...) before any op.get_context()/op.get_bind() usage.","Ensure both online and offline env.py branches reach configure().","Run migrations through the standard command path that wires the context."],"tags":["alembic","configuration","env-setup","migrations"],"analyzedSha":"44fb3450330204b222ff05135e1fbbbdb28c44db","analyzedAt":"2026-08-04T19:57:10.248Z","schemaVersion":2}