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

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8113
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 resolution deterministic when schema keys collide with custom values
  • prefer exact schema key before alias resolution in
  • 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 colliding with OAuth

Why

When is set to and OAuth is also present (often remapped to ), adapters can resolve model/field lookups against the wrong table, causing runtime errors like and invalid joins.

Validation

RUN v4.0.18 /Users/bowzee/pinqwin/better-auth/packages/core

✓ src/db/test/model-resolution-collision.test.ts (3 tests) 2ms

Test Files 1 passed (1)
Tests 3 passed (3)
Start at 00:15:36
Duration 206ms (transform 86ms, setup 0ms, import 105ms, tests 2ms, environment 1ms)

  • full test run currently fails on existing unrelated import issue in (), reproduced locally without touching that area.

Summary by cubic

Make model and field resolution deterministic when schema keys collide with custom modelName aliases (e.g., user → account alongside OAuth account). Prevents wrong table lookups, broken joins, and runtime errors.

  • Bug Fixes
    • Prefer exact schema key before alias mapping or plural handling.
    • Resolve fields on the explicit model if it has the field; otherwise fall back to alias, then default.
    • Fix join direction by matching FK references against both model keys and modelName aliases; use exact keys in join/where and FK checks.
    • Add regression test for user/account collisions and OAuth account fields.

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/8113 **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 resolution deterministic when schema keys collide with custom values - prefer exact schema key before alias resolution in - 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 colliding with OAuth ## Why When is set to and OAuth is also present (often remapped to ), adapters can resolve model/field lookups against the wrong table, causing runtime errors like and invalid joins. ## Validation - RUN v4.0.18 /Users/bowzee/pinqwin/better-auth/packages/core ✓ src/db/test/model-resolution-collision.test.ts (3 tests) 2ms Test Files 1 passed (1) Tests 3 passed (3) Start at 00:15:36 Duration 206ms (transform 86ms, setup 0ms, import 105ms, tests 2ms, environment 1ms) ✅ - full test run currently fails on existing unrelated import issue in (), reproduced locally without touching that area. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Make model and field resolution deterministic when schema keys collide with custom modelName aliases (e.g., user → account alongside OAuth account). Prevents wrong table lookups, broken joins, and runtime errors. - **Bug Fixes** - Prefer exact schema key before alias mapping or plural handling. - Resolve fields on the explicit model if it has the field; otherwise fall back to alias, then default. - Fix join direction by matching FK references against both model keys and modelName aliases; use exact keys in join/where and FK checks. - Add regression test for user/account collisions and OAuth account fields. <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-15 22:29:51 -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#24654