Skip to content

Fix remote_side for composite self-referential FK targeting a UNIQUE constraint (#484) - #485

Merged
sheinbergon merged 1 commit into
agronholm:masterfrom
NixBiks:fix/484-composite-self-fk-unique-target
Aug 30, 2026
Merged

Fix remote_side for composite self-referential FK targeting a UNIQUE constraint (#484)#485
sheinbergon merged 1 commit into
agronholm:masterfrom
NixBiks:fix/484-composite-self-fk-unique-target

Conversation

@NixBiks

@NixBiks NixBiks commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Changes

Fixes #484.

When a table has a self-referential foreign key that targets a UNIQUE constraint instead of the primary key, the generated remote_side on the many-to-one side named the primary key columns. Those columns take no part in the join, so mapper configuration failed:

sqlalchemy.exc.ArgumentError: Relationship Category.category could not determine any
unambiguous local/remote column pairs based on join condition and remote_side arguments.

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 built remote_side from constraint.referred_table.primary_key rather than from the columns the foreign key refers to. It now uses constraint.elements, so remote_side names the referred columns. Where the foreign key does target the primary key the two are the same, and the output does not change.

Checklist

  • You've added tests (in tests/) which would fail without your patch — test_onetomany_selfref_composite_unique_target in tests/test_generator_declarative.py, covering a composite self-referential foreign key against a UNIQUE constraint. It fails on master with remote_side=[id] and passes with the fix. Full suite: 158 passed; pre-commit clean.
  • You've added a new changelog entry (in CHANGES.rst).

@coveralls

coveralls commented Aug 28, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 97.798% (+0.004%) from 97.794% — NixBiks:fix/484-composite-self-fk-unique-target into agronholm:master

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
NixBiks force-pushed the fix/484-composite-self-fk-unique-target branch from 347e913 to f04d417 Compare August 28, 2026 08:36
@sheinbergon

Copy link
Copy Markdown
Collaborator

10x @NixBiks

@sheinbergon
sheinbergon merged commit 7c7eb5b into agronholm:master Aug 30, 2026
8 checks passed
@agronholm

Copy link
Copy Markdown
Owner

Thanks!

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.

Composite self-referential FK targeting a UNIQUE constraint generates wrong remote_side, mappers fail to configure

4 participants