Skip to content

Render one CHECK for each StrEnum and Literal column - #740

Merged
czpython merged 1 commit into
mainfrom
commonzenpython/one-check-per-choice-column
Sep 27, 2026
Merged

czpython merged 1 commit into
mainfrom
commonzenpython/one-check-per-choice-column

Conversation

@czpython

@czpython czpython commented Sep 27, 2026 •

Copy link
Copy Markdown
Owner

With SQLAlchemy 2.1, druks makemigrations renders the CHECK of a StrEnum or Literal column two more times, as explicit sa.CheckConstraint entries in op.create_table. The column's sa.Enum(..., create_constraint=True) already creates that CHECK. When you apply the revision, each column gets three CHECKs. For a StrEnum, the CHECKs have the same name, so the upgrade fails with DuplicateObject.

Alembic finds a CHECK that a type owns through constraint._create_rule.target. SQLAlchemy 2.1 changed _create_rule to a functools.partial, which has no target. The platform render_item hook now skips each CHECK that has _type_bound set, so only the column type creates the CHECK. A CHECK that a model declares in __table_args__ still renders.

A new test renders the create_table op for a StrEnum column and a Literal column through the hook, and asserts that the output has no CheckConstraint.

@czpython
czpython force-pushed the commonzenpython/one-check-per-choice-column branch from 6ea72e2 to 533058c Compare September 27, 2026 17:38
@czpython
czpython merged commit c3d21c3 into main Sep 27, 2026
3 checks passed
@czpython
czpython deleted the commonzenpython/one-check-per-choice-column branch September 27, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant