[PR #8115] [CLOSED] fix(core): deterministic model/field resolution for user/account collisions #33355

Closed
opened 2026-04-17 23:59:14 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8115
Author: @dilame
Created: 2/23/2026
Status: Closed

Base: canaryHead: codex/fix-deterministic-model-resolution


📝 Commits (2)

  • ffca3bd fix(core): make adapter model resolution deterministic for user/account collisions
  • d3918e0 fix(core): resolve joins by matching references against model keys and aliases

📊 Changes

6 files changed (+155 additions, -14 deletions)

View changed files

📝 packages/core/src/db/adapter/factory.ts (+17 -5)
📝 packages/core/src/db/adapter/get-default-field-name.ts (+17 -1)
📝 packages/core/src/db/adapter/get-default-model-name.ts (+9 -6)
📝 packages/core/src/db/adapter/get-field-attributes.ts (+11 -1)
📝 packages/core/src/db/adapter/get-field-name.ts (+10 -1)
packages/core/src/db/test/model-resolution-collision.test.ts (+91 -0)

📄 Description

Summary

  • make adapter model/field resolution deterministic when schema keys collide with custom modelName values
  • prefer exact schema key before alias resolution in getDefaultModelName
  • resolve fields using explicit model key first, then mapped model alias when required (without global schema guessing)
  • update adapter join/where internals to use exact schema keys when already provided
  • add regression test for user.modelName = "account" colliding with OAuth account

Why

When user.modelName is set to account and OAuth account is also present (often remapped to identity), adapters can resolve model/field lookups against the wrong table. This causes runtime errors such as Field email/accountId not found in model ... and invalid joins.

Validation

  • pnpm --filter @better-auth/core exec vitest run src/db/test/model-resolution-collision.test.ts
  • full @better-auth/core test run currently fails on an existing unrelated import issue in request-state.test.ts (@better-auth/core/async_hooks), reproduced locally without touching that area

Summary by cubic

Makes model and field resolution deterministic when custom modelName overlaps with schema keys (e.g., user.modelName = "account"), preventing wrong table lookups and invalid joins.

  • Bug Fixes
    • Prefer exact schema key before alias resolution in getDefaultModelName (including plural handling).
    • Resolve fields via explicit model key first; only fall back to mapped alias if needed.
    • Match joins by comparing references against both model keys and aliases; remove global schema guessing.
    • Use exact schema keys in adapter where/join logic when provided; add regression test for user/account collision.

Written for commit d3918e023c. 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/8115 **Author:** [@dilame](https://github.com/dilame) **Created:** 2/23/2026 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `codex/fix-deterministic-model-resolution` --- ### 📝 Commits (2) - [`ffca3bd`](https://github.com/better-auth/better-auth/commit/ffca3bd79cfa6cc3a750525e830eed296e7fa023) fix(core): make adapter model resolution deterministic for user/account collisions - [`d3918e0`](https://github.com/better-auth/better-auth/commit/d3918e023c6b8046e1b2bf804afb9ceb9da194a9) fix(core): resolve joins by matching references against model keys and aliases ### 📊 Changes **6 files changed** (+155 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `packages/core/src/db/adapter/factory.ts` (+17 -5) 📝 `packages/core/src/db/adapter/get-default-field-name.ts` (+17 -1) 📝 `packages/core/src/db/adapter/get-default-model-name.ts` (+9 -6) 📝 `packages/core/src/db/adapter/get-field-attributes.ts` (+11 -1) 📝 `packages/core/src/db/adapter/get-field-name.ts` (+10 -1) ➕ `packages/core/src/db/test/model-resolution-collision.test.ts` (+91 -0) </details> ### 📄 Description ## Summary - make adapter model/field resolution deterministic when schema keys collide with custom `modelName` values - prefer exact schema key before alias resolution in `getDefaultModelName` - resolve fields using explicit model key first, then mapped model alias when required (without global schema guessing) - update adapter join/where internals to use exact schema keys when already provided - add regression test for `user.modelName = "account"` colliding with OAuth `account` ## Why When `user.modelName` is set to `account` and OAuth `account` is also present (often remapped to `identity`), adapters can resolve model/field lookups against the wrong table. This causes runtime errors such as `Field email/accountId not found in model ...` and invalid joins. ## Validation - `pnpm --filter @better-auth/core exec vitest run src/db/test/model-resolution-collision.test.ts` ✅ - full `@better-auth/core` test run currently fails on an existing unrelated import issue in `request-state.test.ts` (`@better-auth/core/async_hooks`), reproduced locally without touching that area <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Makes model and field resolution deterministic when custom modelName overlaps with schema keys (e.g., user.modelName = "account"), preventing wrong table lookups and invalid joins. - **Bug Fixes** - Prefer exact schema key before alias resolution in getDefaultModelName (including plural handling). - Resolve fields via explicit model key first; only fall back to mapped alias if needed. - Match joins by comparing references against both model keys and aliases; remove global schema guessing. - Use exact schema keys in adapter where/join logic when provided; add regression test for user/account collision. <sup>Written for commit d3918e023c6b8046e1b2bf804afb9ceb9da194a9. 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-17 23:59:14 -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#33355