{"record":{"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/5551b5d35f985c99cb8f1af2b3c526b050e4c059/alembic/runtime/environment.py#L1037-L1073","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ndef run_migrations_online():\n    bind = context.get_bind()  # called before configure -> raises\n    context.configure(connection=bind)\n// after\ndef run_migrations_online():\n    connectable = engine\n    with connectable.connect() as connection:\n        context.configure(connection=connection)\n        with context.begin_transaction():\n            context.run_migrations()","handlingStrategy":"validation","validationCode":"# Ensure configure() ran before touching context bindings.\ndef configured():\n    from alembic.runtime.environment import EnvironmentContext\n    return EnvironmentContext._migration_context is not None\n\nif not configured():\n    raise RuntimeError('Call context.configure(...) before get_bind()/run_migrations()')\nbind = context.get_bind()","typeGuard":"def context_is_configured(context) -> bool:\n    return getattr(context, '_migration_context', None) is not None","tryCatchPattern":null,"preventionTips":["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."],"tags":["alembic","environment","configuration","database-migration"],"backgroundTag":null,"analyzedSha":"5551b5d35f985c99cb8f1af2b3c526b050e4c059","analyzedAt":"2026-08-11T01:38:46.612Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}