[GH-ISSUE #4564] DB: Incorrect Field Mapping #9983

Closed
opened 2026-04-13 05:50:38 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @0xJJW on GitHub (Sep 10, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/4564

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

TLDR: Incorrect mapping in getTables()

5b78469864/packages/better-auth/src/db/get-tables.ts (L228)

Reproduction steps

  1. Add a custom field name for refreshTokenExpiresAt
export const auth = betterAuth({
  account: {
    fields: {
      accessTokenExpiresAt: "access_token_expires_at",
      refreshTokenExpiresAt: "refresh_token_expires_at",
    },
  },
});
  1. Depending on the adapter you are using you will see different results:

Kysely:
The refresh_token_expires_at column will be missing from the database.

Prisma:
The generated schema will include two @map() values for the previous field which will prevent you from running migrate.

Error: Prisma schema validation - (validate wasm)
Error code: P1012
error: Attribute "@map" can only be defined once.
  -->  prisma/schema.prisma:62
   | 
61 |   id_token                String?   @map("idToken")
62 |   access_token_expires_at DateTime? @map("accessTokenExpiresAt") @map("refreshTokenExpiresAt")
   | 

Current vs. Expected behavior

Current:

  1. Migration fails when using Prisma || Kysely DB missing refresh_token_expires_at

Expected:

  1. Migrations run successfully
  2. Account table should include refresh_token_expires_at

What version of Better Auth are you using?

1.3.6

System info

N/A
Issue pointed out in `current`

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  account: {
    fields: {
      accessTokenExpiresAt: "access_token_expires_at",
      refreshTokenExpiresAt: "refresh_token_expires_at",
    },
  },
});

Additional context

No response

Originally created by @0xJJW on GitHub (Sep 10, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/4564 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce TLDR: Incorrect mapping in `getTables()` https://github.com/better-auth/better-auth/blob/5b784698641bc68d7ad3ea537e6d564a44e60878/packages/better-auth/src/db/get-tables.ts#L228 **Reproduction steps** 1. Add a custom field name for `refreshTokenExpiresAt` ``` export const auth = betterAuth({ account: { fields: { accessTokenExpiresAt: "access_token_expires_at", refreshTokenExpiresAt: "refresh_token_expires_at", }, }, }); ``` 2. Depending on the adapter you are using you will see different results: **Kysely:** The `refresh_token_expires_at` column will be missing from the database. **Prisma:** The generated schema will include two `@map()` values for the previous field which will prevent you from running migrate. ``` Error: Prisma schema validation - (validate wasm) Error code: P1012 error: Attribute "@map" can only be defined once. --> prisma/schema.prisma:62 | 61 | id_token String? @map("idToken") 62 | access_token_expires_at DateTime? @map("accessTokenExpiresAt") @map("refreshTokenExpiresAt") | ``` ### Current vs. Expected behavior Current: 1. Migration fails when using Prisma || Kysely DB missing `refresh_token_expires_at` Expected: 1. Migrations run successfully 2. Account table should include `refresh_token_expires_at` ### What version of Better Auth are you using? 1.3.6 ### System info ```bash N/A Issue pointed out in `current` ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ account: { fields: { accessTokenExpiresAt: "access_token_expires_at", refreshTokenExpiresAt: "refresh_token_expires_at", }, }, }); ``` ### Additional context _No response_
GiteaMirror added the locked label 2026-04-13 05:50:38 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 10, 2025):

This is a confirmed bug in Better Auth v1.3.6: in getTables(), the fieldName for refreshTokenExpiresAt is incorrectly set to the custom value for accessTokenExpiresAt, causing both fields to map to the same database column. This results in the missing refresh_token_expires_at column with Kysely and duplicate @map attributes in Prisma, which breaks migrations and schema validation. Other account fields use the correct mapping logic—this is specific to refreshTokenExpiresAt.

Recent pull requests have improved schema generation and custom field handling, but none explicitly fix this mapping bug for refreshTokenExpiresAt in getTables() as of the latest code reviewed. No official workaround or documentation exists for this issue yet. You may want to monitor future releases or patch the mapping logic locally until an upstream fix lands.
See code reference

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

<!-- gh-comment-id:3275186666 --> @dosubot[bot] commented on GitHub (Sep 10, 2025): <!-- Answer --> This is a confirmed bug in Better Auth v1.3.6: in getTables(), the fieldName for refreshTokenExpiresAt is incorrectly set to the custom value for accessTokenExpiresAt, causing both fields to map to the same database column. This results in the missing refresh_token_expires_at column with Kysely and duplicate @map attributes in Prisma, which breaks migrations and schema validation. Other account fields use the correct mapping logic—this is specific to refreshTokenExpiresAt. Recent pull requests have improved schema generation and custom field handling, but none explicitly fix this mapping bug for refreshTokenExpiresAt in getTables() as of the latest code reviewed. No official workaround or documentation exists for this issue yet. You may want to monitor future releases or patch the mapping logic locally until an upstream fix lands. [See code reference](https://github.com/better-auth/better-auth/blob/84c5f563af9a019ae33138ff612646e1d760abdb/packages/better-auth/src/db/get-tables.ts) <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/b5770515-99b5-4607-9537-3476143abcb9?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/b5770515-99b5-4607-9537-3476143abcb9?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/b5770515-99b5-4607-9537-3476143abcb9?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/b5770515-99b5-4607-9537-3476143abcb9?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/b5770515-99b5-4607-9537-3476143abcb9?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/b5770515-99b5-4607-9537-3476143abcb9?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/b5770515-99b5-4607-9537-3476143abcb9?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/4564)
Author
Owner

@himself65 commented on GitHub (Sep 10, 2025):

have you tried with latest better-auth?

<!-- gh-comment-id:3275890502 --> @himself65 commented on GitHub (Sep 10, 2025): have you tried with latest better-auth?
Author
Owner

@0xJJW commented on GitHub (Sep 10, 2025):

@himself65, I have not tested the latest version as we run a custom implementation of most BetterAuth plugins, and I won't be updating until later this week.

As highlighted in the TLDR, I have pointed out the cause of the issue, which is still present in the canary branch.

5b78469864/packages/better-auth/src/db/get-tables.ts (L224)

I would have created a PR, but my previous PR took a long time to merge (~4 months), so I thought it would be faster to raise it as an issue. @ping-maxwell usually fixes small issues like this before I create a branch. He is a 🧙.

There are probably a fair few BetterAuth implementations out in the wild where the refreshTokenExpiresAt column is missing from the database due to this issue.

<!-- gh-comment-id:3276034670 --> @0xJJW commented on GitHub (Sep 10, 2025): @himself65, I have not tested the latest version as we run a custom implementation of most BetterAuth plugins, and I won't be updating until later this week. As highlighted in the **TLDR**, I have pointed out the cause of the issue, which is still present in the **canary** branch. https://github.com/better-auth/better-auth/blob/5b784698641bc68d7ad3ea537e6d564a44e60878/packages/better-auth/src/db/get-tables.ts#L224 I would have created a PR, but my previous PR took a long time to merge (~4 months), so I thought it would be faster to raise it as an issue. @ping-maxwell usually fixes small issues like this before I create a branch. He is a 🧙. There are probably a fair few BetterAuth implementations out in the wild where the `refreshTokenExpiresAt` column is missing from the database due to this issue.
Author
Owner

@0xJJW commented on GitHub (Sep 10, 2025):

Here is a screenshot from canary

Image
<!-- gh-comment-id:3276043284 --> @0xJJW commented on GitHub (Sep 10, 2025): Here is a screenshot from canary <img width="447" height="150" alt="Image" src="https://github.com/user-attachments/assets/06d6635d-4b21-460f-9424-805f3c480819" />
Author
Owner

@himself65 commented on GitHub (Sep 10, 2025):

oh, I see the codeline. let me do a quick fix

<!-- gh-comment-id:3276043701 --> @himself65 commented on GitHub (Sep 10, 2025): oh, I see the codeline. let me do a quick fix
Author
Owner

@0xJJW commented on GitHub (Sep 10, 2025):

I appreciate it, thank you @himself65 🙌

<!-- gh-comment-id:3276047360 --> @0xJJW commented on GitHub (Sep 10, 2025): I appreciate it, thank you @himself65 🙌
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9983