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.
Description
Fix TypeError when querying primary key metadata using reflection.
Related issue #22
Stack dump excerpt and error:
Solution
The fix modifies
base.py
so that the return type fromget_primary_keys
(wrapped byget_pk_constraint
) is now a dictionary (versus a list) and contains the primary key (i.e. constrained column) name.Note: This solution works with SQLAlchemy version 2.0.27. I was unable to test with SQLAlchemy version 1.4.51 due to an unrelated error that will be addressed in a separate issue.
Testing
Packages and test cases used to verify the fix.
Without the fix, each test case failed with
TypeError: list indices must be integers or slices, not str
.With the fix in place, each test case ran successfully.
Versions of selected Python packages used in testing
Test case 1
test1.py.txt (based on the example in comment)
Test case 2
test2.py.txt (based on the example in comment)
Notable SQLAlchemy related changes
changelog_06.rst - May 16, 2022
changelog_08.rst - Oct 2, 2022
changelog_14.rst - Jan 2, 2024
Question to consider
Would it be valuable for
get_pk_constraint/get_primary_keys
to also return the constraint name (in addition to the column name)? I checked theiikeys
table of my Actian X 11.2 data database and found these examples of constraint names for defined primary keys:$user__U0000025B0000, $addre_U0000025E0000, $famil_U000002E90000
I don't know whether these constraint names would be at all useful when retrieving primary key metadata.