[PR #5887] [CLOSED] fix(adpaters/drizzle-adapter) use primary db connection for transactions #6297

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5887
Author: @JacobSchroder
Created: 11/10/2025
Status: Closed

Base: canaryHead: fix/drizzle-adapter-use-primary-db-conn-for-transactions


📝 Commits (2)

  • 48e5909 fix(adpaters/drizzle-adapter) use primary db conn for transactions
  • 6146dbd Merge branch 'canary' into fix/drizzle-adapter-use-primary-db-conn-for-transactions

📊 Changes

1 file changed (+2 additions, -1 deletions)

View changed files

📝 packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts (+2 -1)

📄 Description

When using drizzle with read replicas, transactions that have both read and write operations seem to use both connections (primary and read-only), resulting in the writes not being reflected on to the read-only instances (before the transaction is committed).

For instance, when using the emailOTP plugin, and users signed up using this method the following would happen within the same drizzle transaction:

  1. The user would be created (primary/writer instance)
  2. Read the newly created user (read replica) > TypeError: null is not an object (evaluating 'user.banned')

I did a workaround in my application using better-auth and the drizzle-adapter, by passing the primary db instance to the drizzleAdapter drizzleAdapter(db.$primary, ...), which fixed the issue, but completely disables using read replicas with better-auth.

This PR should solve the issue, while still allowing for the use of read replicas (when transactions are not used, internally)


Summary by cubic

Run transactions on the primary DB connection in the Drizzle adapter (falling back to the default when no primary exists). This avoids mixed read/write transactions hitting read replicas and missing uncommitted writes (e.g., email OTP user reads returning null).

Written for commit 6146dbda00. Summary will update automatically on new commits.


🔄 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/5887 **Author:** [@JacobSchroder](https://github.com/JacobSchroder) **Created:** 11/10/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `fix/drizzle-adapter-use-primary-db-conn-for-transactions` --- ### 📝 Commits (2) - [`48e5909`](https://github.com/better-auth/better-auth/commit/48e5909e33e7bd327b89f5c9b0918b2841b53b77) fix(adpaters/drizzle-adapter) use primary db conn for transactions - [`6146dbd`](https://github.com/better-auth/better-auth/commit/6146dbda001744ceac88ee5297bc905c5ded8f68) Merge branch 'canary' into fix/drizzle-adapter-use-primary-db-conn-for-transactions ### 📊 Changes **1 file changed** (+2 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts` (+2 -1) </details> ### 📄 Description When using drizzle with [read replicas](https://orm.drizzle.team/docs/read-replicas), transactions that have both read and write operations seem to use both connections (primary and read-only), resulting in the writes not being reflected on to the read-only instances (before the transaction is committed). For instance, when using the emailOTP plugin, and users signed up using this method the following would happen within the same drizzle transaction: 1. The user would be created (primary/writer instance) 2. Read the newly created user (read replica) > `TypeError: null is not an object (evaluating 'user.banned')` I did a workaround in my application using better-auth and the drizzle-adapter, by passing the primary db instance to the drizzleAdapter `drizzleAdapter(db.$primary, ...)`, which fixed the issue, but completely disables using read replicas with better-auth. This PR should solve the issue, while still allowing for the use of read replicas (when transactions are not used, internally) <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Run transactions on the primary DB connection in the Drizzle adapter (falling back to the default when no primary exists). This avoids mixed read/write transactions hitting read replicas and missing uncommitted writes (e.g., email OTP user reads returning null). <sup>Written for commit 6146dbda001744ceac88ee5297bc905c5ded8f68. Summary will update automatically on new commits.</sup> <!-- 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:53:52 -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#6297