{"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/44fb3450330204b222ff05135e1fbbbdb28c44db/alembic/runtime/migration.py#L251-L287","documentation":"Raised by MigrationContext.configure() when none of connection, url, or dialect_name is provided (and no dialect was passed). The configure factory needs at least one of these to determine the database dialect; without it there is no way to build a MigrationContext.","triggerScenarios":"Calling MigrationContext.configure() with no connection/url/dialect_name; an env.py whose offline branch forgets to pass url= or dialect_name=; passing all three as None because of a config read failure.","commonSituations":"Broken env.py that doesn't resolve the sqlalchemy.url from alembic.ini; misconfigured config object whose get_main_option('sqlalchemy.url') returns None; programmatic use of MigrationContext.configure() with missing arguments.","solutions":["Pass a live Connection (online) or url= (offline) to MigrationContext.configure().","Ensure alembic.ini has a valid sqlalchemy.url, or that env.py sets one programmatically before configure().","If using dialect_name only (e.g. for SQL generation), pass a string like 'postgresql' or 'sqlite'."],"exampleFix":"// before\ncontext = MigrationContext.configure()  # raises Exception\n\n// after\n# online\ncontext = MigrationContext.configure(connection=my_connection)\n# offline\ncontext = MigrationContext.configure(dialect_name='postgresql', opts={...})","handlingStrategy":"validation","validationCode":"def has_dialect_source(connection=None, url=None, dialect_name=None, dialect=None) -> bool:\n    return any([connection, url, dialect_name, dialect])","typeGuard":null,"tryCatchPattern":"try:\n    ctx = MigrationContext.configure(connection=connection, url=url, dialect_name=dn)\nexcept Exception as e:\n    if 'Connection, url, or dialect_name is required' in str(e):\n        ctx = MigrationContext.configure(dialect_name='sqlite')  # explicit fallback\n    else:\n        raise","preventionTips":["Always pass connection (online) or url/dialect_name (offline) to configure().","Validate sqlalchemy.url is present in config before configure().","Set a dialect_name explicitly for offline SQL generation."],"tags":["alembic","configuration","env-setup","migrations","database-connection"],"analyzedSha":"44fb3450330204b222ff05135e1fbbbdb28c44db","analyzedAt":"2026-08-04T19:57:10.248Z","schemaVersion":2}