Skip to content

fix(queryset): hydrate select_related by field order - #2270

Open
vyrnsynx wants to merge 2 commits into
tortoise:developfrom
vyrnsynx:fix/postgres-long-join-alias-1902
Open

vyrnsynx wants to merge 2 commits into
tortoise:developfrom
vyrnsynx:fix/postgres-long-join-alias-1902

Conversation

@vyrnsynx

@vyrnsynx vyrnsynx commented Sep 2, 2026

Copy link
Copy Markdown

Description

Postgres truncates identifiers to 63 bytes. Tortoise select_related JOINs build aliases such as table__long_relation__nested.field and execute_select used to recover field names by splitting those aliases on a dot. After truncation this raises IndexError (the separator is gone) or hydrates the wrong attribute.

Related columns were already sliced by position via select_related_idx. This change records the ordered field names for each related group when the query is built and zips them with row values, so field identity no longer depends on untruncated aliases. SQLite and MySQL keep the same SELECT shape; only hydration mapping changes.

Motivation and Context

Fixes #1902

How Has This Been Tested?

  • New regression tests for nested long FK aliases, long OneToOne model plus column names, and only plus select_related
  • Existing select_related, only, and relations tests on SQLite
  • Helper test that positional row access survives duplicate truncated keys

Checklist:

  • My code follows the code style of this project.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Postgres truncates identifiers to 63 bytes, which breaks parsing
field names from long JOIN aliases in execute_select. Record each
related group's field names when the query is built and zip them
positionally so long OneToOne and nested FK names work.

Fixes tortoise#1902

Co-authored-by: MarkGus0 <MarkGus0@users.noreply.github.com>
@codspeed

codspeed Bot commented Sep 5, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 33.03%

⚡ 1 improved benchmark
✅ 23 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_hydrate_100_rows_many_fields 17.1 ms 12.8 ms +33.03%

Tip

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


Comparing MarkGus0:fix/postgres-long-join-alias-1902 (ed6d105) with develop (ffc946e)

Open in CodSpeed

Nested lookups re-selected the parent relation columns while
_select_related_idx kept a single entry, so positional hydration
mis-sliced rows and set phantom related objects.
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.

Tortoise queries with long JOIN aliases raise exceptions when using Postgres as the backend database

1 participant