[GH-ISSUE #8111] Model name collision when user.modelName is "account" causes wrong FK targets in generated schema #28320

Open
opened 2026-04-17 19:44:48 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @dilame on GitHub (Feb 23, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/8111

Summary

When user.modelName is set to "account" and OAuth table is renamed (for example account.modelName = "identity"), generated schema can resolve references incorrectly due to model-key/modelName collision.

Reproduction

betterAuth({
  user: { modelName: "account" },
  account: { modelName: "identity" },
  session: { fields: { userId: "account_id" } },
})

Run better-auth generate (or migrations path).

Expected

  • session.account_id references account(id) (user table)
  • identity.account_id references account(id) (user table)

Actual

In affected builds, references can resolve to wrong target table because resolver prioritizes schema key over modelName match in collision scenarios.

Additional context

This reproduces consistently in a real project with custom snake_case fields and custom model names used to avoid reserved words.

Originally created by @dilame on GitHub (Feb 23, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/8111 ### Summary When `user.modelName` is set to `"account"` and OAuth table is renamed (for example `account.modelName = "identity"`), generated schema can resolve references incorrectly due to model-key/modelName collision. ### Reproduction ```ts betterAuth({ user: { modelName: "account" }, account: { modelName: "identity" }, session: { fields: { userId: "account_id" } }, }) ``` Run `better-auth generate` (or migrations path). ### Expected - `session.account_id` references `account(id)` (user table) - `identity.account_id` references `account(id)` (user table) ### Actual In affected builds, references can resolve to wrong target table because resolver prioritizes schema key over `modelName` match in collision scenarios. ### Related PR - https://github.com/better-auth/better-auth/pull/8110 ### Additional context This reproduces consistently in a real project with custom snake_case fields and custom model names used to avoid reserved words.
GiteaMirror added the needs: infocoredatabasebug labels 2026-04-17 19:44:48 -05:00
Author
Owner

@ping-maxwell commented on GitHub (Feb 23, 2026):

Hey @dilame does this issue only affect generated schemas? after fixing the schema manually, does Better Auth work fine with your configurations?

<!-- gh-comment-id:3946253128 --> @ping-maxwell commented on GitHub (Feb 23, 2026): Hey @dilame does this issue only affect generated schemas? after fixing the schema manually, does Better Auth work fine with your configurations?
Author
Owner

@dilame commented on GitHub (Feb 23, 2026):

Hi @ping-maxwell

From our side, this was not only a generated-schema problem.

What we observed:

  1. With the original config (user.modelName = "account" and OAuth account remapped), we hit runtime adapter errors (Field ... not found in model ...) even after schema adjustments.
  2. We patched resolution logic locally and got partial progress (e.g. sign-up/sign-in could work), but then hit join-resolution issues (No foreign key found for model account and base model user) and session retrieval inconsistencies depending on the exact mapping/schema state.
  3. Renaming schema objects manually (e.g. moving toward default user model + identity for OAuth account) improved behavior, but did not invalidate the core finding: model key vs modelName alias collisions in adapter resolution are the root issue.

So in short: generated SQL is part of the pain, but runtime resolution (model/field/join lookup under collisions) is the main issue we were trying to fix.

<!-- gh-comment-id:3946286274 --> @dilame commented on GitHub (Feb 23, 2026): Hi @ping-maxwell From our side, this was not only a generated-schema problem. What we observed: 1) With the original config (`user.modelName = "account"` and OAuth `account` remapped), we hit runtime adapter errors (`Field ... not found in model ...`) even after schema adjustments. 2) We patched resolution logic locally and got partial progress (e.g. sign-up/sign-in could work), but then hit join-resolution issues (`No foreign key found for model account and base model user`) and session retrieval inconsistencies depending on the exact mapping/schema state. 3) Renaming schema objects manually (e.g. moving toward default `user` model + `identity` for OAuth account) improved behavior, but did not invalidate the core finding: model key vs modelName alias collisions in adapter resolution are the root issue. So in short: generated SQL is part of the pain, but runtime resolution (model/field/join lookup under collisions) is the main issue we were trying to fix.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#28320