Skip to content

Invalid autogeneration: deferrable="True" #1613

@tanguy-michardiere

Description

@tanguy-michardiere

Describe the bug
When autogenerating a migration for a table with a UniqueConstraint that specifies deferrable=True, the resulting UniqueConstraint in the migration script specifies deferrable="True". Mypy complains: error: Argument "deferrable" to "UniqueConstraint" has incompatible type "str"; expected "bool | None" [arg-type]

Expected behavior
The generated UniqueConstraint should specify deferrable=True.

To Reproduce

# model.py

from uuid import UUID

from sqlalchemy import UniqueConstraint
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column


class Base(DeclarativeBase):
    id: Mapped[UUID] = mapped_column(primary_key=True)


class Model(Base):
    __tablename__ = "model"

    key: Mapped[str]

    __table_args__ = (UniqueConstraint("key", deferrable=True, initially="DEFERRED"),)
# version.py

import sqlalchemy as sa
from alembic import op


def upgrade() -> None:
    op.create_table(
        "model",
        sa.Column("key", sa.String(), nullable=False),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("key", deferrable="True", initially="DEFERRED"),
    )


def downgrade() -> None:
    op.drop_table("model")

Error

error: Argument "deferrable" to "UniqueConstraint" has incompatible type "str"; expected "bool | None"  [arg-type]
            sa.UniqueConstraint("key", deferrable="True", initially="DEFERRED"),
                                                  ^~~~~~

Versions.

  • OS: macOS Sequoia 15.3.1
  • Python: 3.13.2
  • Alembic: 1.14.1
  • SQLAlchemy: 2.0.38
  • Database: PostgreSQL 17.2
  • DBAPI: psycopg

Additional context
From a basic search in the source code, I believe the error may be here

Have a nice day!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions