Support postgresql_include for create_primary_key / create_unique_constraint#1819
Open
anneheartrecord wants to merge 1 commit into
Open
Conversation
…straint The postgresql_include option added in SQLAlchemy 2.0.41 was only handled for create_index. Passing it to create_primary_key raised a TypeError as the operation did not accept dialect-specific keyword arguments, and create_unique_constraint raised a KeyError during DDL compilation because the INCLUDE columns were absent from the synthetic table built for the ALTER statement. create_primary_key now accepts dialect-specific keyword arguments, and the PostgreSQL implementation appends any postgresql_include columns referenced by name to the constraint's table before emitting AddConstraint, mirroring the existing create_index handling.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1723
Problem
postgresql_include(added in SQLAlchemy 2.0.41 forPrimaryKeyConstraint/UniqueConstraint) was only wired up forcreate_index. Using it via the op directives failed in two distinct ways:op.create_primary_key(..., postgresql_include=[...])raisedTypeError: create_primary_key() got an unexpected keyword argument 'postgresql_include'because the public operation method did not accept dialect-specific keyword arguments.op.create_unique_constraint(..., postgresql_include=[...])raisedKeyErrorduring DDL compilation: SQLAlchemy's_define_includelooks the INCLUDE columns up inconstraint.table.c, but those columns are not present on the synthetic table Alembic builds for theALTER ... ADD CONSTRAINTstatement.Fix
CreatePrimaryKeyOp.create_primary_keynow accepts**kw, so dialect-specific kwargs flow through to the constraint (the rest of the op pipeline —__init__,to_constraint,from_constraint, andSchemaObjects.primary_key_constraint— already passed them through).create_unique_constraintalready accepted**kw.PostgresqlImpl.add_constraintnow appends anypostgresql_includecolumns referenced by name to the constraint's synthetic table before emittingAddConstraint, mirroring the existing handling increate_index. The shared logic is extracted into a small_ensure_include_columnshelper used by bothcreate_indexandadd_constraint..pyi/base.pyoperation stubs viatools/write_pyi.py(validated bytests/test_stubs.py).docs/build/unreleased/1723.rst.Tests
Added
test_create_unique_constraint_postgresql_includeandtest_create_primary_key_postgresql_includetotests/test_postgresql.py, asserting the emitted DDL:Both fail on
main(TypeError / KeyError respectively) and pass with this change. Full suite: 1769 passed, 129 skipped (DB-specific). flake8, black, mypy, and the stub-consistency test all pass.