Skip to content

Invalid autogeneration: deferrable="True" #1613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tanguyMichardiere opened this issue Feb 26, 2025 · 2 comments
Closed

Invalid autogeneration: deferrable="True" #1613

tanguyMichardiere opened this issue Feb 26, 2025 · 2 comments
Labels
autogenerate - rendering bug Something isn't working

Comments

@tanguyMichardiere
Copy link

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!

@tanguyMichardiere tanguyMichardiere added the requires triage New issue that requires categorization label Feb 26, 2025
@sqla-tester
Copy link
Collaborator

Mike Bayer has proposed a fix for this issue in the main branch:

dont stringify uq deferrable https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/5729

@zzzeek zzzeek added bug Something isn't working autogenerate - rendering and removed requires triage New issue that requires categorization labels Feb 26, 2025
@zzzeek
Copy link
Member

zzzeek commented Feb 26, 2025

will be in the next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autogenerate - rendering bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants