[PR #8117] [MERGED] fix(drizzle-adapter): resolve db.query key mismatch when config.schema keys differ from schema exports #24656

Closed
opened 2026-04-15 22:29:55 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8117
Author: @typedrat
Created: 2/23/2026
Status: Merged
Merged: 2/28/2026
Merged by: @Bekacru

Base: canaryHead: fix/drizzle-adapter-use-plural


📝 Commits (1)

  • 5440036 fix(drizzle): resolve db.query key mismatch when config.schema keys differ from schema exports

📊 Changes

2 files changed (+302 additions, -4 deletions)

View changed files

e2e/adapter/test/drizzle-adapter/adapter.drizzle.plural-joins.test.ts (+259 -0)
📝 packages/drizzle-adapter/src/drizzle-adapter.ts (+43 -4)

📄 Description

When users pass singular model names in config.schema (e.g. { user: schema.users }) but drizzle() receives plural schema exports, db.query is keyed by the export names ("users"), not the config.schema keys ("user"). The join code path did db.query[model] directly, which errored with "model not found in the query object" when the keys didn't match.

Add getQueryModel() helper that scans config.schema to find the matching db.query key via table object identity when direct lookup fails.


Summary by cubic

Fixes Drizzle adapter join queries when config.schema uses singular model names but the Drizzle schema exports are plural. Joins now resolve the correct db.query key and return related data as expected.

  • Bug Fixes
    • Added getQueryModel() to resolve the db.query key by trying the model name, an optional plural (when usePlural is true), and finally matching the table object from config.schema against db._.fullSchema.
    • Updated findOne/findMany join paths to use the resolved key; logs once and falls back only if no match is found.
    • Added an e2e regression test covering singular config keys with plural schema exports to ensure joins return related records.

Written for commit 5440036c98. Summary will update 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/8117 **Author:** [@typedrat](https://github.com/typedrat) **Created:** 2/23/2026 **Status:** ✅ Merged **Merged:** 2/28/2026 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `fix/drizzle-adapter-use-plural` --- ### 📝 Commits (1) - [`5440036`](https://github.com/better-auth/better-auth/commit/5440036c98e66d8ed6fc501653a4429b252c73a6) fix(drizzle): resolve db.query key mismatch when config.schema keys differ from schema exports ### 📊 Changes **2 files changed** (+302 additions, -4 deletions) <details> <summary>View changed files</summary> ➕ `e2e/adapter/test/drizzle-adapter/adapter.drizzle.plural-joins.test.ts` (+259 -0) 📝 `packages/drizzle-adapter/src/drizzle-adapter.ts` (+43 -4) </details> ### 📄 Description When users pass singular model names in config.schema (e.g. { user: schema.users }) but drizzle() receives plural schema exports, db.query is keyed by the export names ("users"), not the config.schema keys ("user"). The join code path did db.query[model] directly, which errored with "model not found in the query object" when the keys didn't match. Add getQueryModel() helper that scans config.schema to find the matching db.query key via table object identity when direct lookup fails. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes Drizzle adapter join queries when config.schema uses singular model names but the Drizzle schema exports are plural. Joins now resolve the correct db.query key and return related data as expected. - **Bug Fixes** - Added getQueryModel() to resolve the db.query key by trying the model name, an optional plural (when usePlural is true), and finally matching the table object from config.schema against db._.fullSchema. - Updated findOne/findMany join paths to use the resolved key; logs once and falls back only if no match is found. - Added an e2e regression test covering singular config keys with plural schema exports to ensure joins return related records. <sup>Written for commit 5440036c98e66d8ed6fc501653a4429b252c73a6. Summary will update 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-04-15 22:29:55 -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#24656