[PR #8110] [CLOSED] fix(core): avoid user modelName collision with account model #15998

Closed
opened 2026-04-13 10:20:37 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

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

Base: canaryHead: codex/fix-user-modelname-account-collision


📝 Commits (3)

  • 76595e3 fix(core): avoid user modelName collision with account model
  • a3efe64 chore(core): use USER_MODEL_KEY fallback for user modelName
  • 4ffb14d fix(core): resolve model key by modelName first to avoid account collisions

📊 Changes

4 files changed (+50 additions, -9 deletions)

View changed files

📝 packages/core/src/db/adapter/get-default-model-name.ts (+6 -6)
📝 packages/core/src/db/get-tables.ts (+5 -3)
packages/core/src/db/test/get-default-model-name.test.ts (+23 -0)
📝 packages/core/src/db/test/get-tables.test.ts (+16 -0)

📄 Description

Summary

Fixes a schema/model-name collision when configuring:

  • user.modelName = "account"
  • account.modelName = "identity"

Previously, references for session.userId and account.userId were built from options.user.modelName, which caused collisions with the built-in account model key and produced incorrect references.

This patch makes those references point to the base user schema key ("user"), preserving the logical relation to the user model regardless of modelName aliases.

What changed

  • packages/core/src/db/get-tables.ts

    • use the base user model key for references in:
      • session.fields.userId.references.model
      • account.fields.userId.references.model
  • packages/core/src/db/test/get-tables.test.ts

    • add regression test for:
      • user.modelName = "account"
      • account.modelName = "identity"
    • assert:
      • custom model names are preserved
      • session.userId.references.model === "user"
      • account.userId.references.model === "user"

Reproduction (before fix)

Configure Better Auth with:

  • user.modelName = "account"
  • account.modelName = "identity"

Then generate schema: references may target the wrong table due to model-name collision.

Expected

References that represent "user ownership" should always resolve against the user schema key, not be redirected by modelName collisions.

Validation

Ran:

pnpm --filter @better-auth/core exec vitest run src/db/test/get-tables.test.ts

<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Fixes model-name collisions when user.modelName overlaps with account. User references now always point to the base "user" schema, and default model key resolution prefers modelName over schema key.

- **Bug Fixes**
  - Always use the base "user" model key for session.userId and account.userId references.
  - Prefer modelName over schema key when resolving default model keys (avoids "account"/"identity" collisions).
  - Preserve custom modelName aliases for user and account.

- **Refactors**
  - Use USER_MODEL_KEY constant as the fallback for user.modelName.

<sup>Written for commit 4ffb14d313b4e5b89faff860b42451122c69b32b. 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>
## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/8110 **Author:** [@dilame](https://github.com/dilame) **Created:** 2/23/2026 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `codex/fix-user-modelname-account-collision` --- ### 📝 Commits (3) - [`76595e3`](https://github.com/better-auth/better-auth/commit/76595e357196cbde5ba4bdbae14508cd3ef16d68) fix(core): avoid user modelName collision with account model - [`a3efe64`](https://github.com/better-auth/better-auth/commit/a3efe64fe41ee2c76c15047395860ff565ed52df) chore(core): use USER_MODEL_KEY fallback for user modelName - [`4ffb14d`](https://github.com/better-auth/better-auth/commit/4ffb14d313b4e5b89faff860b42451122c69b32b) fix(core): resolve model key by modelName first to avoid account collisions ### 📊 Changes **4 files changed** (+50 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `packages/core/src/db/adapter/get-default-model-name.ts` (+6 -6) 📝 `packages/core/src/db/get-tables.ts` (+5 -3) ➕ `packages/core/src/db/test/get-default-model-name.test.ts` (+23 -0) 📝 `packages/core/src/db/test/get-tables.test.ts` (+16 -0) </details> ### 📄 Description ## Summary Fixes a schema/model-name collision when configuring: - `user.modelName = "account"` - `account.modelName = "identity"` Previously, references for `session.userId` and `account.userId` were built from `options.user.modelName`, which caused collisions with the built-in `account` model key and produced incorrect references. This patch makes those references point to the base user schema key (`"user"`), preserving the logical relation to the user model regardless of `modelName` aliases. ## What changed - `packages/core/src/db/get-tables.ts` - use the base user model key for references in: - `session.fields.userId.references.model` - `account.fields.userId.references.model` - `packages/core/src/db/test/get-tables.test.ts` - add regression test for: - `user.modelName = "account"` - `account.modelName = "identity"` - assert: - custom model names are preserved - `session.userId.references.model === "user"` - `account.userId.references.model === "user"` ## Reproduction (before fix) Configure Better Auth with: - `user.modelName = "account"` - `account.modelName = "identity"` Then generate schema: references may target the wrong table due to model-name collision. ## Expected References that represent "user ownership" should always resolve against the user schema key, not be redirected by `modelName` collisions. ## Validation Ran: ```bash pnpm --filter @better-auth/core exec vitest run src/db/test/get-tables.test.ts <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes model-name collisions when user.modelName overlaps with account. User references now always point to the base "user" schema, and default model key resolution prefers modelName over schema key. - **Bug Fixes** - Always use the base "user" model key for session.userId and account.userId references. - Prefer modelName over schema key when resolving default model keys (avoids "account"/"identity" collisions). - Preserve custom modelName aliases for user and account. - **Refactors** - Use USER_MODEL_KEY constant as the fallback for user.modelName. <sup>Written for commit 4ffb14d313b4e5b89faff860b42451122c69b32b. 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-13 10:20:37 -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#15998