Skip to content

fix(queryset): hydrate select_related by field order (Fixes #1902) - #1

Closed
vyrnsynx wants to merge 1 commit into
developfrom
fix/postgres-long-join-alias-1902
Closed

vyrnsynx wants to merge 1 commit into
developfrom
fix/postgres-long-join-alias-1902

Conversation

@vyrnsynx

@vyrnsynx vyrnsynx commented Sep 2, 2026

Copy link
Copy Markdown
Owner

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 .. After truncation this raises IndexError (the . is gone) or hydrates the wrong attribute (AttributeError on long OneToOne / column names).

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 tortoise#1902

How Has This Been Tested?

  • New regression tests for nested long FK aliases, long OneToOne model + column names, and .only() + select_related
  • Existing select_related / .only() / 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.
Open in Web Open in Cursor 

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>
@vyrnsynx

vyrnsynx commented Sep 2, 2026

Copy link
Copy Markdown
Owner Author

Superseded by tortoise#2270

@vyrnsynx vyrnsynx closed this Sep 2, 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.

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

1 participant