[PR #5004] [MERGED] fix(adapter): use updated field values in WHERE clause during update #5710

Closed
opened 2026-03-13 12:32:32 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5004
Author: @QuintenStr
Created: 9/30/2025
Status: Merged
Merged: 10/2/2025
Merged by: @himself65

Base: canaryHead: fix/drizzle-mysql-adapter-update-in-where


📝 Commits (10+)

  • 20a3711 fix(drizzle-mysql-adapter): use updated field values in WHERE clause during update
  • 8027500 Merge branch 'canary' into fix/drizzle-mysql-adapter-update-in-where
  • 6e2e41a Merge branch 'canary' into fix/drizzle-mysql-adapter-update-in-where
  • 7b38f2a Merge branch 'canary' into fix/drizzle-mysql-adapter-update-in-where
  • 5c180e0 fix(drizzle-mysql-adapter): tests
  • 31d41ae Merge branch 'fix/drizzle-mysql-adapter-update-in-where' of https://github.com/QuintenStr/better-auth into fix/drizzle-mysql-adapter-update-in-where
  • 55aa4ad chore: removed abundant test that was not compatible with prisma (where needs unique selector)
  • 40e63fb Merge branch 'canary' into fix/drizzle-mysql-adapter-update-in-where
  • 3d0c82b Merge branch 'canary' into fix/drizzle-mysql-adapter-update-in-where
  • b04d503 Merge branch 'canary' into fix/drizzle-mysql-adapter-update-in-where

📊 Changes

2 files changed (+81 additions, -1 deletions)

View changed files

📝 packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts (+10 -1)
📝 packages/better-auth/src/adapters/tests/normal.ts (+71 -0)

📄 Description

Clearly describe what changes you made and why

I modified the logic that builds the WHERE clause during updates to ensure that if a field appears in both the data payload and the existing where conditions, the updated value is used for the lookup.

Previously, when updating a record and using fields that were also part of the WHERE filter (e.g. updating a unique identifier or key field), the query builder would still use the old value — potentially resulting in no record being matched or, worse, unexpected updates.

One known issue occurred with the MySQL-Drizzle adapter, where updatedUser returned null after sending a verification email when a verified user attempted to change their email address.

See: sendVerificationEmail error during the email change process #3088

Include any relevant context or background

/packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts:
Previous behaviour:

const clause = convertWhereClause(where, model);

New behaviour:

// If we're updating a field that's in the where clause, use the new value
const updatedWhere = where.map((w) => {
	// If this field was updated, use the new value for lookup
	if (data[w.field] !== undefined) {
		return { ...w, value: data[w.field] };
	}
	return w;
});
const clause = convertWhereClause(updatedWhere, model);

List any breaking changes or deprecations

No breaking changes found. All tests still run.

Add screenshots for UI changes

No UI changes.

See: sendVerificationEmail error during the email change process #3088


Summary by cubic

Use updated field values in the WHERE clause during update operations in the Drizzle MySQL adapter. This prevents missed matches when changing key fields and resolves the email change flow bug where updatedUser could be null (#3088).


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/5004 **Author:** [@QuintenStr](https://github.com/QuintenStr) **Created:** 9/30/2025 **Status:** ✅ Merged **Merged:** 10/2/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/drizzle-mysql-adapter-update-in-where` --- ### 📝 Commits (10+) - [`20a3711`](https://github.com/better-auth/better-auth/commit/20a37115526c8911c03a9bc159c20489214f6d8a) fix(drizzle-mysql-adapter): use updated field values in WHERE clause during update - [`8027500`](https://github.com/better-auth/better-auth/commit/8027500c1a69b2619676903a7b7d5619902d9451) Merge branch 'canary' into fix/drizzle-mysql-adapter-update-in-where - [`6e2e41a`](https://github.com/better-auth/better-auth/commit/6e2e41ad8953226db85d551b0a4a572bcf386932) Merge branch 'canary' into fix/drizzle-mysql-adapter-update-in-where - [`7b38f2a`](https://github.com/better-auth/better-auth/commit/7b38f2a6aa80f9af9fe04e4945611f6d01e8aa4f) Merge branch 'canary' into fix/drizzle-mysql-adapter-update-in-where - [`5c180e0`](https://github.com/better-auth/better-auth/commit/5c180e0d132e394167be1281c798fe67c3910588) fix(drizzle-mysql-adapter): tests - [`31d41ae`](https://github.com/better-auth/better-auth/commit/31d41ae7ca98ec8cd57448988703bac64e18a916) Merge branch 'fix/drizzle-mysql-adapter-update-in-where' of https://github.com/QuintenStr/better-auth into fix/drizzle-mysql-adapter-update-in-where - [`55aa4ad`](https://github.com/better-auth/better-auth/commit/55aa4ad9b389df7df55ceab9231f69b2395cf6d0) chore: removed abundant test that was not compatible with prisma (where needs unique selector) - [`40e63fb`](https://github.com/better-auth/better-auth/commit/40e63fb6a855c3c1a7fcbd1dc26d987892ef6118) Merge branch 'canary' into fix/drizzle-mysql-adapter-update-in-where - [`3d0c82b`](https://github.com/better-auth/better-auth/commit/3d0c82bcd0569bc926bcee1b2d17c10b015d9462) Merge branch 'canary' into fix/drizzle-mysql-adapter-update-in-where - [`b04d503`](https://github.com/better-auth/better-auth/commit/b04d5034295fa8346a14a2a04fff04df824f5082) Merge branch 'canary' into fix/drizzle-mysql-adapter-update-in-where ### 📊 Changes **2 files changed** (+81 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts` (+10 -1) 📝 `packages/better-auth/src/adapters/tests/normal.ts` (+71 -0) </details> ### 📄 Description ### Clearly describe what changes you made and why I modified the logic that builds the `WHERE` clause during updates to ensure that if a field appears in both the `data` payload *and* the existing `where` conditions, the updated value is used for the lookup. Previously, when updating a record and using fields that were also part of the `WHERE` filter (e.g. updating a unique identifier or key field), the query builder would still use the **old value** — potentially resulting in no record being matched or, worse, unexpected updates. One known issue occurred with the MySQL-Drizzle adapter, where updatedUser returned null after sending a verification email when a verified user attempted to change their email address. See: [sendVerificationEmail error during the email change process #3088 ](https://github.com/better-auth/better-auth/issues/3088) ### Include any relevant context or background /packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts: Previous behaviour: ``` const clause = convertWhereClause(where, model); ``` New behaviour: ``` // If we're updating a field that's in the where clause, use the new value const updatedWhere = where.map((w) => { // If this field was updated, use the new value for lookup if (data[w.field] !== undefined) { return { ...w, value: data[w.field] }; } return w; }); const clause = convertWhereClause(updatedWhere, model); ``` ### List any breaking changes or deprecations No breaking changes found. All tests still run. ### Add screenshots for UI changes No UI changes. ### Reference related issues or discussions See: [sendVerificationEmail error during the email change process #3088 ](https://github.com/better-auth/better-auth/issues/3088) <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Use updated field values in the WHERE clause during update operations in the Drizzle MySQL adapter. This prevents missed matches when changing key fields and resolves the email change flow bug where updatedUser could be null (#3088). <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 12:32:32 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#5710