Repository navigation
fix(sqlalchemy-spanner): include the constraint name in reflected primary keys - #18845
Open
rocksnow1942 wants to merge 2 commits into
Open
rocksnow1942 wants to merge 2 commits into
rocksnow1942 wants to merge 2 commits into
Conversation
…mary keys get_pk_constraint() and get_multi_pk_constraint() returned primary keys without the "name" key that ReflectedPrimaryKeyConstraint requires. Alembic 1.18 and later read it during autogenerate, which then failed with KeyError: 'name' on any database that has a table. Return the constraint name. Fixes googleapis#18841
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the Spanner dialect's get_multi_pk_constraint method to fetch and include the primary key constraint name (tc.constraint_name) in the returned dictionary, which is necessary for Alembic autogeneration. Unit and integration tests have been added to verify this behavior. The review feedback suggests optimizing the dictionary lookup in get_multi_pk_constraint by replacing dict.get() with a membership check to avoid unnecessary dictionary and list allocations on each iteration.
…t_multi_pk_constraint Instead of building a default dict for every row.
rocksnow1942
marked this pull request as ready for review
October 24, 2026 09:53
This branch has not been deployed
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.
get_pk_constraint()andget_multi_pk_constraint()returned primary keys as{"constrained_columns": [...]}, without thenamekey that SQLAlchemy'sReflectedPrimaryKeyConstraintrequires. Since Alembic 1.18.0, autogenerate loads reflection data with theget_multi_*()methods and readsconst["name"]from each constraint, soalembic revision --autogenerateandalembic checkfailed withKeyError: 'name'on any Spanner database that has a table. #18841 has a repro.This PR selects
tc.constraint_nameinget_multi_pk_constraint()and returns it asname. Spanner names primary keysPK_<table>.The existing
test_get_multi_pk_constraintcompliance test didn't catch this, because the Spanner override of_check_listonly compares keys that are in both dicts, so this also addstest_get_pk_constraint_name.Tested with the unit and mockserver tests, the compliance suite against the emulator, and the Alembic 1.20 repro from #18841.
Fixes #18841