orm: forward-port join-order fix from v9.2.1 to v10 - #2048
Open
OscarYuen wants to merge 1 commit into
Open
Conversation
Forward-port of 83b9413 ("Change join order"), which landed on v9 for v9.2.1 and was never carried over to v10. In manyQuery and m2mQuery the user-supplied ApplyQuery hook — the callback form of Relation("Name", func(q *orm.Query) (*orm.Query, error)) — runs immediately after q.Model(), before the join appends the "FK IN (parent ids)" predicate and before the polymorphic condition. The default-column decision (len(q.columns) == 0) is likewise made before those are in place. Moving both to the end of the function makes the hook observe the fully constructed relation query, which is what v9.2.1 does. orm, types, internal and internal/pool tests pass; gofmt clean.
OscarYuen
force-pushed
the
fix/forward-port-join-order-to-v10
branch
from
August 21, 2026 02:19
238ee79 to
9f8eabb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
83b941300e97("Change join order") landed on thev9branch and shipped in v9.2.1 (2021-04-22). It was never forward-ported tov10, so every v10 release — up to and including v10.15.1 — still has the pre-fix ordering.This applies the same change to
v10.What the ordering does
In
manyQueryandm2mQuery, the user-suppliedApplyQueryhook runs immediately afterq.Model(...):So a callback passed as:
sees a query that has not yet been scoped to the parent rows, and the
len(q.columns) == 0check that decides whether to add the defaulthas-many columns is evaluated at the same early point.
After this change both run at the end of the function, once the relation
query is fully constructed — matching v9.2.1.
Diff
Pure code motion in
orm/join.go: theApplyQueryblock and theadjacent default-column block move from just after
q.Model(...)to justbefore
return q, nil, in bothmanyQueryandm2mQuery. No behaviouris added or removed beyond the ordering. 24 insertions, 22 deletions, one
file.
Verification
go build ./...cleango test ./orm/... ./types/... ./internal/...— all passgofmtcleanmanyQuery/m2mQueryordering is identical tov9@v9.2.1I don't have a Postgres instance wired up here, so the DB-backed
integration tests were not exercised — worth a CI run.