Skip to content

Fetch the unchanged model in patchAndFetch() and updateAndFetch() with an empty patch - #518

Merged
lehni merged 1 commit into
mainfrom
fix/517
Oct 18, 2026
Merged

lehni merged 1 commit into
mainfrom
fix/517

Conversation

@lehni

@lehni lehni commented Oct 18, 2026 •

Copy link
Copy Markdown

patchAndFetchById(id, {}) and the other *AndFetch* methods returned undefined for an existing row when the patch was empty. Since 1.5.0, an update with nothing to write isn't run and resolves with 0, and the fetch read that 0 as "no rows matched".

An empty update now still fetches. As nothing was written, the fetch uses the find form of the update, the same query the static hooks get from asFindQuery(), so the row comes back only if it matches the update's filters, e.g. where() or the relation of $relatedQuery(). Updates that write something fetch by id as before, since a write can change the filtered columns. A builder onBuild() that selects columns applies to the fetch of an empty update; the fetch of a write copies only explicit select() calls, as before.

The find form had a bug of its own, also in asFindQuery() of static after hooks since 2.0.0: it was taken from the built write query, which already holds what the onBuild hooks of its context added, and they ran again when the find query was built, so e.g. a context join was added twice ("ambiguous column name"). The query now remembers which operations its context's onBuild hooks added during the build, and toFindQuery() leaves them out. toFindQuery() also clears the write's runAfter() callbacks, resolve value and fetched graph itself, which asFindQuery() did before, so both callers share it.

Tests cover patchAndFetchById(), updateAndFetchById(), $query().patchAndFetch() and a many-to-many $relatedQuery().patchAndFetchById(), each with a matching and a non-matching row, an empty patch with a joining context onBuild, and asFindQuery() in afterUpdate, afterInsert and afterDelete with a joining context onBuild (afterDelete not on SQLite, which can't delete with joins). They fail without the fix.

Closes #517

@lehni
lehni force-pushed the fix/517 branch 4 times, most recently from af264ae to 5992394 Compare October 18, 2026 14:08
@lehni
lehni merged commit 652da3f into main Oct 18, 2026
7 checks passed
@lehni
lehni deleted the fix/517 branch October 18, 2026 14:22
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.

patchAndFetchById() with an empty patch returns undefined for existing rows

1 participant