[GH-ISSUE #2899] getDefaultFieldName in database adapter is not properly returning the default field name #18015

Closed
opened 2026-04-15 16:22:17 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @raymonddaikon on GitHub (Jun 4, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2899

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Running the getDefaultFieldName supplied by the adapter function in createAdapter does not return the default field name when supplied with a custom field name.

Current vs. Expected behavior

On my account schema I renamed the "userId" field to "user". When running user in the provided function, user is returned. Looking at the current implementation not working is expected. Here's my modified function that will correctly return the default field name

 const getDefaultFieldName = ({
          field,
          model: unsafe_model,
        }: { model: string; field: string }) => {
          // Plugin `schema`s can't define their own `id`. Better-auth auto provides `id` to every schema model.
          // Given this, we can't just check if the `field` (that being `id`) is within the schema's fields, since it is never defined.
          // So we check if the `field` is `id` and if so, we return `id` itself. Otherwise, we return the `field` from the schema.
          if (field === "id") {
            return field;
          }
          const model = getDefaultModelName(unsafe_model); // Just to make sure the model name is correct.

          let f = schema[model]?.fields[field];
          if (!f) {
            debugLog(`Field ${field} not found in model ${model}`);
            debugLog(`Schema:`, schema);
            // Need to pull in the key value from the user options.
            return Object.keys(options[model as keyof BetterAuthOptions]?.fields).find((key) => options[model as keyof BetterAuthOptions]?.fields[key] === field)!
          }
          return field;
        };

What version of Better Auth are you using?

1.2.8

Provide environment information

N/A

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
    account: {
      modelName: 'account',
      fields: {
        userId: 'user',
        accountId: 'account_id',
        providerId: 'provider_id',
        accessToken: 'access_token',
        refreshToken: 'refresh_token',
        accessTokenExpiresAt: 'access_token_expires',
        refreshTokenExpiresAt: 'refresh_token_expires',
        idToken: 'id_token',
        createdAt: 'created',
        updatedAt: 'updated',
      },
      accountLinking: {
        enabled: true,
        trustedProviders: ["google", "github", "email-password"],
        allowDifferentEmails: false
      },
      updateAccountOnSignIn: true
    },
});

Additional context

No response

Originally created by @raymonddaikon on GitHub (Jun 4, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2899 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Running the getDefaultFieldName supplied by the adapter function in createAdapter does not return the default field name when supplied with a custom field name. ### Current vs. Expected behavior On my account schema I renamed the "userId" field to "user". When running user in the provided function, user is returned. Looking at the current [implementation](https://github.com/better-auth/better-auth/blob/e96e07ed7547f7828b4c57615355486d7e3e0011/packages/better-auth/src/adapters/create-adapter/index.ts#L88) not working is expected. Here's my modified function that will correctly return the default field name ```typescript const getDefaultFieldName = ({ field, model: unsafe_model, }: { model: string; field: string }) => { // Plugin `schema`s can't define their own `id`. Better-auth auto provides `id` to every schema model. // Given this, we can't just check if the `field` (that being `id`) is within the schema's fields, since it is never defined. // So we check if the `field` is `id` and if so, we return `id` itself. Otherwise, we return the `field` from the schema. if (field === "id") { return field; } const model = getDefaultModelName(unsafe_model); // Just to make sure the model name is correct. let f = schema[model]?.fields[field]; if (!f) { debugLog(`Field ${field} not found in model ${model}`); debugLog(`Schema:`, schema); // Need to pull in the key value from the user options. return Object.keys(options[model as keyof BetterAuthOptions]?.fields).find((key) => options[model as keyof BetterAuthOptions]?.fields[key] === field)! } return field; }; ``` ### What version of Better Auth are you using? 1.2.8 ### Provide environment information ```bash N/A ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, account: { modelName: 'account', fields: { userId: 'user', accountId: 'account_id', providerId: 'provider_id', accessToken: 'access_token', refreshToken: 'refresh_token', accessTokenExpiresAt: 'access_token_expires', refreshTokenExpiresAt: 'refresh_token_expires', idToken: 'id_token', createdAt: 'created', updatedAt: 'updated', }, accountLinking: { enabled: true, trustedProviders: ["google", "github", "email-password"], allowDifferentEmails: false }, updateAccountOnSignIn: true }, }); ``` ### Additional context _No response_
GiteaMirror added the locked label 2026-04-15 16:22:17 -05:00
Author
Owner

@ping-maxwell commented on GitHub (Jul 8, 2025):

@raymonddaikon can you double check for me that latest version of BA still has this issue?

<!-- gh-comment-id:3049355337 --> @ping-maxwell commented on GitHub (Jul 8, 2025): @raymonddaikon can you double check for me that latest version of BA still has this issue?
Author
Owner

@ping-maxwell commented on GitHub (Jul 12, 2025):

Hey I'm going to assume you're on an out-dated version, and will be closing this issue.
Please let me know if I'm wrong and I'll happily open it and help

<!-- gh-comment-id:3065893949 --> @ping-maxwell commented on GitHub (Jul 12, 2025): Hey I'm going to assume you're on an out-dated version, and will be closing this issue. Please let me know if I'm wrong and I'll happily open it and help
Author
Owner

@raymonddaikon commented on GitHub (Jul 14, 2025):

Hey sorry for the late reply, I can confirm as of version 1.2.12 it still does not work I need to override the function with my own implementation from my initial message. @ping-maxwell
Edit: Just checked with 1.3.0 beta 8 and it still is broken, when logging in it will throw Cannot read properties of undefined (reading 'fieldName')

<!-- gh-comment-id:3067510726 --> @raymonddaikon commented on GitHub (Jul 14, 2025): Hey sorry for the late reply, I can confirm as of version 1.2.12 it still does not work I need to override the function with my own implementation from my initial message. @ping-maxwell Edit: Just checked with 1.3.0 beta 8 and it still is broken, when logging in it will throw `Cannot read properties of undefined (reading 'fieldName')`
Author
Owner

@ping-maxwell commented on GitHub (Aug 11, 2025):

@raymonddaikon This shouldn't be an issue on latest.

<!-- gh-comment-id:3176745882 --> @ping-maxwell commented on GitHub (Aug 11, 2025): @raymonddaikon This shouldn't be an issue on latest.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#18015