Skip to content

fix: derive table aliases from relation paths - #1842

Merged
collerek merged 14 commits into
masterfrom
fix/path-based-table-aliases
Oct 15, 2026
Merged

collerek merged 14 commits into
masterfrom
fix/path-based-table-aliases

Conversation

@collerek

@collerek collerek commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Table aliases are now derived from the relation path (alias_for("aseller__city")) instead of a global per-relation registry with "complex" aliases for duplicated paths. This fixes filters, exclude(), order_by(), fields() / exclude_fields(), values() and prefetch_related() on relation paths that end in the same relation, regardless of call order, and removes the in-place alias switching that let a derived queryset corrupt its source. Duplicate many-to-many paths that failed with "Ambiguous column name" now work, and select_related() on a through model raises a clear error.

Also fixes chained exclude() (based on #1826) and a select_related() crash when a column alias on the related model equals the relation name (based on #1829), both by @moneebullah25.

Closes #1816
Closes #1827

@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 13, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 23.64%

⚠️ 109 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 2 improved benchmarks
✅ 121 untouched benchmarks
🆕 3 new benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ ⚠️ test_sum[500] 8 ms 6 ms +34.49%
⚡ ⚠️ test_avg[250] 8.1 ms 7.1 ms +13.67%
🆕 ⚠️ test_duplicate_relation_paths[100] N/A 156.8 ms N/A
🆕 ⚠️ test_duplicate_relation_paths[200] N/A 299 ms N/A
🆕 ⚠️ test_duplicate_relation_paths[50] N/A 89.7 ms N/A

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing fix/path-based-table-aliases (ef52ba6) with master (1405d58)

Open in CodSpeed

…lumn alias

Co-authored-by: moneebullah25 <moneebullah25@gmail.com>
Table aliases are a pure function of the relation path, so the values() column
map no longer needs a stateful class. It is built once per call as a dict of
table alias to label and allowed columns, and through tables are registered
explicitly instead of being detected by a substring check.
AliasManager only held a static column builder and a cache of aliased tables,
shared as one global instance on every OrmarConfig. Replace it with
prefixed_columns and a cached prefixed_table function, drop
OrmarConfig.alias_manager and rename the module to ormar/relations/aliases.py.
@collerek
collerek merged commit 80c85d7 into master Oct 15, 2026
11 checks passed
@collerek
collerek deleted the fix/path-based-table-aliases branch October 15, 2026 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants