Skip to content

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

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

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

Conversation

@moneebullah25

Copy link
Copy Markdown

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)

This branch has not been deployed

No deployments
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

1 participant