sqlalchemy/alembic · error · Exception

Connection, url, or dialect_name is required.

Error message

Connection, url, or dialect_name is required.

What it means

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.

Source

Thrown at alembic/runtime/migration.py:269

            dialect_opts = {}

        if connection:
            if isinstance(connection, Engine):
                raise util.CommandError(
                    "'connection' argument to configure() is expected "
                    "to be a sqlalchemy.engine.Connection instance, "
                    "got %r" % connection,
                )

            dialect = connection.dialect
        elif url:
            url_obj = sqla_url.make_url(url)
            dialect = url_obj.get_dialect()(**dialect_opts)
        elif dialect_name:
            url_obj = sqla_url.make_url("%s://" % dialect_name)
            dialect = url_obj.get_dialect()(**dialect_opts)
        elif not dialect:
            raise Exception("Connection, url, or dialect_name is required.")
        assert dialect is not None
        return MigrationContext(dialect, connection, opts, environment_context)

    @contextmanager
    def autocommit_block(self) -> Iterator[None]:
        """Enter an "autocommit" block, for databases that support AUTOCOMMIT
        isolation levels.

        This special directive is intended to support the occasional database
        DDL or system operation that specifically has to be run outside of
        any kind of transaction block.   The PostgreSQL database platform
        is the most common target for this style of operation, as many
        of its DDL operations must be run outside of transaction blocks, even
        though the database overall supports transactional DDL.

        The method is used as a context manager within a migration script, by
        calling on :meth:`.Operations.get_context` to retrieve the
        :class:`.MigrationContext`, then invoking

View on GitHub (pinned to 44fb345033)

Solutions

  1. Pass a live Connection (online) or url= (offline) to MigrationContext.configure().
  2. Ensure alembic.ini has a valid sqlalchemy.url, or that env.py sets one programmatically before configure().
  3. If using dialect_name only (e.g. for SQL generation), pass a string like 'postgresql' or 'sqlite'.

Example fix

// before
context = MigrationContext.configure()  # raises Exception

// after
# online
context = MigrationContext.configure(connection=my_connection)
# offline
context = MigrationContext.configure(dialect_name='postgresql', opts={...})
Defensive patterns

Strategy: validation

Validate before calling

def has_dialect_source(connection=None, url=None, dialect_name=None, dialect=None) -> bool:
    return any([connection, url, dialect_name, dialect])

Try / catch

try:
    ctx = MigrationContext.configure(connection=connection, url=url, dialect_name=dn)
except Exception as e:
    if 'Connection, url, or dialect_name is required' in str(e):
        ctx = MigrationContext.configure(dialect_name='sqlite')  # explicit fallback
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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