{"record":{"id":"7964e79cc8ea4974","repo":"sqlalchemy/alembic","slug":"connection-url-or-dialect-name-is-required","errorCode":null,"errorMessage":"Connection, url, or dialect_name is required.","messagePattern":"Connection, url, or dialect_name is required\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"alembic/runtime/migration.py","lineNumber":269,"sourceCode":"            dialect_opts = {}\n\n        if connection:\n            if isinstance(connection, Engine):\n                raise util.CommandError(\n                    \"'connection' argument to configure() is expected \"\n                    \"to be a sqlalchemy.engine.Connection instance, \"\n                    \"got %r\" % connection,\n                )\n\n            dialect = connection.dialect\n        elif url:\n            url_obj = sqla_url.make_url(url)\n            dialect = url_obj.get_dialect()(**dialect_opts)\n        elif dialect_name:\n            url_obj = sqla_url.make_url(\"%s://\" % dialect_name)\n            dialect = url_obj.get_dialect()(**dialect_opts)\n        elif not dialect:\n            raise Exception(\"Connection, url, or dialect_name is required.\")\n        assert dialect is not None\n        return MigrationContext(dialect, connection, opts, environment_context)\n\n    @contextmanager\n    def autocommit_block(self) -> Iterator[None]:\n        \"\"\"Enter an \"autocommit\" block, for databases that support AUTOCOMMIT\n        isolation levels.\n\n        This special directive is intended to support the occasional database\n        DDL or system operation that specifically has to be run outside of\n        any kind of transaction block.   The PostgreSQL database platform\n        is the most common target for this style of operation, as many\n        of its DDL operations must be run outside of transaction blocks, even\n        though the database overall supports transactional DDL.\n\n        The method is used as a context manager within a migration script, by\n        calling on :meth:`.Operations.get_context` to retrieve the\n        :class:`.MigrationContext`, then invoking","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/sqlalchemy/alembic/blob/5551b5d35f985c99cb8f1af2b3c526b050e4c059/alembic/runtime/migration.py#L251-L287","documentation":"Raised as a bare Exception by MigrationContext.configure() (migration.py:248-269) when none of connection, url, or dialect_name was supplied and no dialect could be derived otherwise. The factory builds a dialect from one of those sources (connection.dialect, url.get_dialect(), or makeURL('name://')); if all are absent it cannot proceed.","triggerScenarios":"Calling MigrationContext.configure() with no connection/url/dialect_name; an env.py that, in offline mode, forgets to pass url=config.get_main_option('sqlalchemy.url'); a programmatic caller that constructs MigrationContext directly without specifying how to reach the DB.","commonSituations":"Misconfigured alembic.ini with a blank sqlalchemy.url; offline (offline=True) migrations where the url option is missing; custom command scripts that call configure() with only opts=.","solutions":["Pass one of connection=, url=, or dialect_name= to MigrationContext.configure() (or to EnvironmentContext.configure which forwards it).","In env.py offline branch, supply url=config.get_main_option(\"sqlalchemy.url\").","Ensure alembic.ini has a non-empty sqlalchemy.url (or that the URL is injected via env)."],"exampleFix":"// before (offline)\ncontext.configure(url=None, target_metadata=target_metadata)\n// after\ncontext.configure(\n    url=config.get_main_option(\"sqlalchemy.url\"),\n    target_metadata=target_metadata,\n)","handlingStrategy":"validation","validationCode":"# Verify a dialect source is available before configure().\ndef ensure_dialect_source(connection=None, url=None, dialect_name=None):\n    if not any([connection, url, dialect_name]):\n        raise ValueError(\n            'MigrationContext.configure requires one of connection, url, or dialect_name'\n        )\n\nurl = config.get_main_option('sqlalchemy.url')\nensure_dialect_source(url=url)\ncontext.configure(url=url, target_metadata=target_metadata)","typeGuard":"def has_dialect_source(connection=None, url=None, dialect_name=None) -> bool:\n    return any([connection, url, dialect_name])","tryCatchPattern":null,"preventionTips":["Keep a non-empty sqlalchemy.url in alembic.ini (or inject it from env in env.py).","In the offline branch of env.py, always pass url= to configure().","Validate required config before calling configure() in custom scripts."],"tags":["alembic","configuration","migration-context","database-migration"],"backgroundTag":null,"analyzedSha":"5551b5d35f985c99cb8f1af2b3c526b050e4c059","analyzedAt":"2026-08-11T01:38:46.612Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}