Fix remote_side for composite self-referential FK targeting a UNIQUE constraint (#484) - #485
Merged
sheinbergon merged 1 commit intoAug 30, 2026
Conversation
A self-referential foreign key that references a UNIQUE constraint rather than the primary key produced a remote_side naming the primary key, so mapper configuration failed with ArgumentError. remote_side now names the columns the foreign key actually refers to. Fixes agronholm#484.
NixBiks
force-pushed
the
fix/484-composite-self-fk-unique-target
branch
from
August 28, 2026 08:36
347e913 to
f04d417
Compare
sheinbergon
approved these changes
Aug 30, 2026
Collaborator
|
10x @NixBiks |
Owner
|
Thanks! |
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.
Changes
Fixes #484.
When a table has a self-referential foreign key that targets a
UNIQUEconstraint instead of the primary key, the generatedremote_sideon the many-to-one side named the primary key columns. Those columns take no part in the join, so mapper configuration failed:The generated module still imports cleanly, so an import-only smoke test does not catch this; the error only appears on the first query or on
configure_mappers().The cause is in
DeclarativeGenerator.generate_relationships, which builtremote_sidefromconstraint.referred_table.primary_keyrather than from the columns the foreign key refers to. It now usesconstraint.elements, soremote_sidenames the referred columns. Where the foreign key does target the primary key the two are the same, and the output does not change.Checklist
tests/) which would fail without your patch —test_onetomany_selfref_composite_unique_targetintests/test_generator_declarative.py, covering a composite self-referential foreign key against aUNIQUEconstraint. It fails onmasterwithremote_side=[id]and passes with the fix. Full suite: 158 passed; pre-commit clean.CHANGES.rst).