Skip to content

Throw when a patchAndFetch() or updateAndFetch() query is executed a second time - #520

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

lehni merged 1 commit into
mainfrom
fix/519

Conversation

@lehni

@lehni lehni commented Oct 18, 2026 •

Copy link
Copy Markdown

patchAndFetch(), patchAndFetchById(), updateAndFetch() and updateAndFetchById() merge the fetched row into the model they patch with. Each run works on a copy of the query, but that model is shared, so awaiting the same query builder a second time patched all fetched columns, writing back values that other writes had changed in between (#519, since at least 1.0.0).

A second run of such a query now throws, and the docs of execute() say to create a new query for each run. The query counts as run once the fetched row is merged, so a run that fails before that, e.g. on a deadlock, can be retried. Concurrent runs of the same query aren't detected.

It throws rather than returning a new model, as the issue proposed, because patchAndFetchById() returns the model that was passed in as the patch, which a test pins since 2015. Inserts aren't guarded: they also write their ids into the given models, but re-running them already fails with a unique violation, or works as intended with onConflict().

As this turns a re-awaited patchAndFetch() that mostly worked into an error, it's listed under Changes for 4.2.0.

Closes #519

@lehni lehni changed the title Throw when an insert or *AndFetch() query is executed a second time Throw when a patchAndFetch() or updateAndFetch() query is executed a second time Oct 18, 2026
@lehni
lehni merged commit d1c8fbe into main Oct 18, 2026
7 checks passed
@lehni
lehni deleted the fix/519 branch October 18, 2026 15:28
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.

Awaiting a patchAndFetch() / updateAndFetch() query twice writes back the fetched row

1 participant