Skip to content

fix(orm): preserve Scanner in Query.Clone() - #724

Merged
stephenafamo merged 2 commits into
stephenafamo:mainfrom
sandonemaki:fix/query-clone-scanner
Jul 7, 2026
Merged

fix(orm): preserve Scanner in Query.Clone()#724
stephenafamo merged 2 commits into
stephenafamo:mainfrom
sandonemaki:fix/query-clone-scanner

Conversation

@sandonemaki

Copy link
Copy Markdown
Contributor

Summary

orm.Query.Clone() only clones the embedded ExecQuery and never copies Scanner,
so any cloned query has a nil scanner and All/One/Cursor/Each panic:

q := models.Users.Query()
cloned := q.Query.Clone()
_, err := cloned.All(ctx, exec) // panic: invalid memory address or nil pointer dereference

Fix

Copy the Scanner in Clone() (one line), and add a regression test
(TestQueryClonePreservesScanner) mirroring the existing
TestModQueryWithPreservesScanner. The test fails on main and passes with the fix.

Noticed while working on #715: with the mapper flowing through q.Scanner,
a clone silently dropping it is easier to hit than before.

Query.Clone() only cloned the embedded ExecQuery and dropped the Scanner,
so All/One/Cursor/Each on a cloned query panicked with a nil pointer
dereference. Copy the Scanner in Clone() and add a regression test
mirroring TestModQueryWithPreservesScanner.
@stephenafamo
stephenafamo merged commit aef6f4d into stephenafamo:main Jul 7, 2026
8 checks passed
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.

2 participants