Skip to content

fix(orm): evaluate $onUpdate lazily in buildUpdateSet across dialects#5851

Open
chatman-media wants to merge 1 commit into
drizzle-team:mainfrom
chatman-media:fix/onupdate-lazy-eval
Open

fix(orm): evaluate $onUpdate lazily in buildUpdateSet across dialects#5851
chatman-media wants to merge 1 commit into
drizzle-team:mainfrom
chatman-media:fix/onupdate-lazy-eval

Conversation

@chatman-media
Copy link
Copy Markdown

Closes #5780.

buildUpdateSet computed col.onUpdateFn?.() eagerly for every column, then only used the result when the column was absent from set (set[colName] ?? …). So any $onUpdate callback with side effects — throw, logging, reading external state — ran on every UPDATE, even when the column value was explicitly provided and would win via ??. This is a regression from the lazy 0.44.6 behavior.

const table = pgTable('example', {
  updatedById: uuid('updated_by_id').$onUpdate(() => {
    throw new Error('should not be called when explicitly set');
  }),
  // ...
});

// 0.44.6: ok · 0.45.x: throws
await db.update(table).set({ updatedById: 'some-uuid' }).where(/* … */);

Fix: only evaluate the callback when the column is absent from set (set[colName] == null, matching the existing ?? semantics). The value line is unchanged. Applied to all five dialects that share this code path: pg, mysql, sqlite, singlestore, gel.

Test: added a DB-less unit test (tests/on-update-set.test.ts) — a throwing $onUpdate with an explicit value must not throw, and an absent column must still invoke the callback.

🤖 Generated with Claude Code

buildUpdateSet computed `col.onUpdateFn?.()` eagerly for every column and only
used the result when the column was absent from `set` (via `??`). Any $onUpdate
callback with side effects (throw, logging, reading external state) therefore ran
on every UPDATE, even when the column value was explicitly provided. This
regressed the lazy 0.44.6 behavior.

Only invoke the callback when the column is absent from `set`. Applied to the pg,
mysql, sqlite, singlestore and gel dialects.

Closes drizzle-team#5780
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.

[BUG]: $onUpdate callback invoked eagerly in buildUpdateSet even when column is explicitly provided in set (regression in 0.45.x)

1 participant