Skip to content

Support postgresql_include for create_primary_key / create_unique_constraint#1819

Open
anneheartrecord wants to merge 1 commit into
sqlalchemy:mainfrom
anneheartrecord:fix-1723-pk-unique-postgresql-include
Open

Support postgresql_include for create_primary_key / create_unique_constraint#1819
anneheartrecord wants to merge 1 commit into
sqlalchemy:mainfrom
anneheartrecord:fix-1723-pk-unique-postgresql-include

Conversation

@anneheartrecord

Copy link
Copy Markdown

Fixes #1723

Problem

postgresql_include (added in SQLAlchemy 2.0.41 for PrimaryKeyConstraint / UniqueConstraint) was only wired up for create_index. Using it via the op directives failed in two distinct ways:

  • op.create_primary_key(..., postgresql_include=[...]) raised TypeError: 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=[...]) raised KeyError during DDL compilation: SQLAlchemy's _define_include looks the INCLUDE columns up in constraint.table.c, but those columns are not present on the synthetic table Alembic builds for the ALTER ... ADD CONSTRAINT statement.

Fix

  • CreatePrimaryKeyOp.create_primary_key now accepts **kw, so dialect-specific kwargs flow through to the constraint (the rest of the op pipeline — __init__, to_constraint, from_constraint, and SchemaObjects.primary_key_constraint — already passed them through). create_unique_constraint already accepted **kw.
  • PostgresqlImpl.add_constraint now appends any postgresql_include columns referenced by name to the constraint's synthetic table before emitting AddConstraint, mirroring the existing handling in create_index. The shared logic is extracted into a small _ensure_include_columns helper used by both create_index and add_constraint.
  • Regenerated the .pyi / base.py operation stubs via tools/write_pyi.py (validated by tests/test_stubs.py).
  • Added a changelog fragment at docs/build/unreleased/1723.rst.

Tests

Added test_create_unique_constraint_postgresql_include and test_create_primary_key_postgresql_include to tests/test_postgresql.py, asserting the emitted DDL:

ALTER TABLE t ADD CONSTRAINT uq_t UNIQUE (c1, c2) INCLUDE (inc)
ALTER TABLE t ADD CONSTRAINT pk_t PRIMARY KEY (c1, c2) INCLUDE (inc)

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.

…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.
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.

Create primary key / unique constraint does not support postgresql_include added in sqlalchemy 2.0.41

1 participant