Skip to content

fix: use relation name as key when loading select_related child in from_row - #1829

Closed
moneebullah25 wants to merge 3 commits into
ormar-orm:masterfrom
moneebullah25:fix/select-related-relation-name-alias
Closed

moneebullah25 wants to merge 3 commits into
ormar-orm:masterfrom
moneebullah25:fix/select-related-relation-name-alias

Conversation

@moneebullah25

Copy link
Copy Markdown
Contributor

Fixes #1827

Problem

select_related on a relation raises ModelError when a column alias on the related model equals the relation name. In ModelRow.from_row the loaded child was stored under model_cls.get_column_name_from_alias(related), which translates the relation name through the related model's column aliases. If a column there has name= equal to the relation name, the child ended up under that field's key instead of the relation's.

Fix

Store the child under related (the relation name) directly in ormar/models/model_row.py.

Tests

Added tests/test_relations/test_relation_name_equals_column_alias.py covering forward ForeignKey, reverse relation and ManyToMany where a column alias equals the relation name.

Checks run

  • ruff format --check and ruff check (ruff 0.16.10) on the changed files: pass
  • mypy ormar tests/test_relations/test_relation_name_equals_column_alias.py --ignore-missing-imports: no issues
  • pytest tests/test_relations tests/test_queries (sqlite): 202 passed, 24 skipped
  • Full test suite with coverage and the other database backends (mysql, postgresql): not run locally, left to CI
  • flake8 ormar and mkdocs build: not run (no documentation changes)

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Oct 12, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 123 untouched benchmarks


Comparing moneebullah25:fix/select-related-relation-name-alias (8a8d282) with master (a8d4861)

Open in CodSpeed

@collerek

Copy link
Copy Markdown
Collaborator

Thanks for the fix and the tests! #1842 rewrites the same part of row parsing, so the fix went in there, together with your reverse-relation and many-to-many test cases (credited in the release notes and the commit). Closing this one in favour of it.

@collerek collerek closed this Oct 13, 2026
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.

select_related on a ForeignKey raises ModelError when a column alias on the related model equals the relation name

3 participants