better auth+prisma+mongodb combo literally doesn't work #1746

Closed
opened 2026-03-13 09:00:29 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @bayromest on GitHub (Aug 20, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

When the psiam.schema is generated using the better auth cli generate command, I was surprised that all the ids didn't have the @db.ObjectId

I ignored that at first glance, but then I hit a wall where I literally can't work anymore..
in my user/[id] page I have a server action that fetches the user by id with prisma. The id is right but I get this issue

The server action;

export async function GetUserById(id: string): Promise<User | null> {
  return prisma.user.findUnique({
    where: { id },
  });
}

The error

Invalid `prisma.user.findUnique()` invocation:


Inconsistent column data: Malformed ObjectID: invalid character 'm' was found at index 0 in the provided hex string: "miXWIaHDA7J4Wm5Edc30qNSlVzpUrjyJ".

This means that the better-auth is assigning IDs the wrong way in all the models it creates.. Adding the @db.ObjectId to the User's ID error's the creationg of the user (also adding it to any model that was generated by better-auth)

Current vs. Expected behavior

The models generated by better-auth when using prisma + mongodb should have the @db.ObjectId next to their IDs and fetching users by IDs should work seemlessly

What version of Better Auth are you using?

1.3.7

System info

System:
   OS: Windows 11 10.0.26100
   CPU: (16) x64 13th Gen Intel(R) Core(TM) i7-13620H
   Memory: 1.07 GB / 15.71 GB
Browsers:
   Edge: Chromium (139.0.3405.102)

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

Types

Auth config (if applicable)

export async function GetUserById(id: string): Promise<User | null> {
  return prisma.user.findUnique({
    where: { id }
  });
}

Additional context

No response

Originally created by @bayromest on GitHub (Aug 20, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce When the psiam.schema is generated using the better auth cli generate command, I was surprised that all the ids didn't have the @db.ObjectId I ignored that at first glance, but then I hit a wall where I literally can't work anymore.. in my user/[id] page I have a server action that fetches the user by id with prisma. The id is right but I get this issue The server action; ```ts export async function GetUserById(id: string): Promise<User | null> { return prisma.user.findUnique({ where: { id }, }); } ``` The error ```bash Invalid `prisma.user.findUnique()` invocation: Inconsistent column data: Malformed ObjectID: invalid character 'm' was found at index 0 in the provided hex string: "miXWIaHDA7J4Wm5Edc30qNSlVzpUrjyJ". ``` This means that the better-auth is assigning IDs the wrong way in all the models it creates.. Adding the @db.ObjectId to the User's ID error's the creationg of the user (also adding it to any model that was generated by better-auth) ### Current vs. Expected behavior The models generated by better-auth when using prisma + mongodb should have the @db.ObjectId next to their IDs and fetching users by IDs should work seemlessly ### What version of Better Auth are you using? 1.3.7 ### System info ```bash System: OS: Windows 11 10.0.26100 CPU: (16) x64 13th Gen Intel(R) Core(TM) i7-13620H Memory: 1.07 GB / 15.71 GB Browsers: Edge: Chromium (139.0.3405.102) ``` ### Which area(s) are affected? (Select all that apply) Types ### Auth config (if applicable) ```typescript export async function GetUserById(id: string): Promise<User | null> { return prisma.user.findUnique({ where: { id } }); } ``` ### Additional context _No response_
GiteaMirror added the bug label 2026-03-13 09:00:29 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 20, 2025):

Hi @bayromest! I'm Dosu and I’m helping the better-auth team.

This is a known issue with Better Auth v1.3.7: the Prisma schema generator does not correctly map the id field to MongoDB's ObjectId, which leads to the error you're seeing when fetching users by ID. The fix for this was merged recently and ensures that, for MongoDB, the generated Prisma schema includes @id @map("_id") on the id field, allowing Prisma to handle ObjectIds properly. This change is not present in v1.3.7, so you'll need to either upgrade to a version that includes PR #3021 (merged 2025-08-06) or manually edit your generated schema to add @id @map("_id") to the id fields of your models.

If you add @db.ObjectId, make sure your IDs are actually ObjectIds in MongoDB; otherwise, creation will fail due to type mismatches. The safest path is to use the updated generator or manually ensure your schema matches what Prisma expects for MongoDB IDs.

Reference: PR #3021 details

To reply, just mention @dosu.


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

@dosubot[bot] commented on GitHub (Aug 20, 2025): <!-- Greeting --> Hi @bayromest! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a known issue with Better Auth v1.3.7: the Prisma schema generator does not correctly map the `id` field to MongoDB's ObjectId, which leads to the error you're seeing when fetching users by ID. The fix for this was merged recently and ensures that, for MongoDB, the generated Prisma schema includes `@id @map("_id")` on the `id` field, allowing Prisma to handle ObjectIds properly. This change is not present in v1.3.7, so you'll need to either upgrade to a version that includes [PR #3021](https://github.com/better-auth/better-auth/pull/3021) (merged 2025-08-06) or manually edit your generated schema to add `@id @map("_id")` to the `id` fields of your models. If you add `@db.ObjectId`, make sure your IDs are actually ObjectIds in MongoDB; otherwise, creation will fail due to type mismatches. The safest path is to use the updated generator or manually ensure your schema matches what Prisma expects for MongoDB IDs. Reference: [PR #3021 details](https://github.com/better-auth/better-auth/pull/3021) <!-- 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/178d456d-b910-4ea7-955b-64058a9bcebb?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/178d456d-b910-4ea7-955b-64058a9bcebb?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/178d456d-b910-4ea7-955b-64058a9bcebb?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/178d456d-b910-4ea7-955b-64058a9bcebb?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/178d456d-b910-4ea7-955b-64058a9bcebb?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/178d456d-b910-4ea7-955b-64058a9bcebb?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/178d456d-b910-4ea7-955b-64058a9bcebb?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/4106)
Author
Owner

@bayromest commented on GitHub (Aug 20, 2025):

yea with @id @map("_id") i still get the issue, the issue is that the models doesn't have @db.ObjectId and the ids aren't generated automatically by mongo instead by better-auth

@bayromest commented on GitHub (Aug 20, 2025): yea with ```@id @map("_id")``` i still get the issue, the issue is that the models doesn't have ```@db.ObjectId``` and the ids aren't generated automatically by mongo instead by better-auth
Author
Owner

@dosubot[bot] commented on GitHub (Aug 20, 2025):

This happens because Better Auth's Prisma schema generator doesn't add @db.ObjectId to the id fields for MongoDB models—it only adds @id @map("_id"), which makes Prisma treat the ID as a string, not an ObjectId. As a result, Better Auth generates string IDs internally, and MongoDB doesn't auto-generate ObjectIds, causing type mismatches and errors when you query by ID source.

To fix this, you need two things:

  1. Manually update your Prisma schema: Change your model's id field to id String @id @map("_id") @db.ObjectId. This tells Prisma to use MongoDB's ObjectId type.
  2. Configure Better Auth to let MongoDB generate IDs: In your Better Auth config, set advanced.database.generateId: false. This disables Better Auth's internal ID generation and lets MongoDB assign ObjectIds automatically source.

Example:

model User {
  id String @id @map("_id") @db.ObjectId
  // ...other fields
}
export const auth = betterAuth({
  database: {
    db: db,
  },
  advanced: {
    database: {
      generateId: false,
    },
  },
  // ...other config
});

After making these changes, MongoDB will generate ObjectIds for new documents, and Prisma will treat them correctly. If you upgrade Better Auth in the future, check if the generator adds @db.ObjectId automatically—current versions do not source.

To reply, just mention @dosu.


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

@dosubot[bot] commented on GitHub (Aug 20, 2025): <!-- Answer --> This happens because Better Auth's Prisma schema generator doesn't add `@db.ObjectId` to the `id` fields for MongoDB models—it only adds `@id @map("_id")`, which makes Prisma treat the ID as a string, not an ObjectId. As a result, Better Auth generates string IDs internally, and MongoDB doesn't auto-generate ObjectIds, causing type mismatches and errors when you query by ID [source](https://github.com/better-auth/better-auth/pull/3021). To fix this, you need two things: 1. **Manually update your Prisma schema**: Change your model's `id` field to `id String @id @map("_id") @db.ObjectId`. This tells Prisma to use MongoDB's ObjectId type. 2. **Configure Better Auth to let MongoDB generate IDs**: In your Better Auth config, set `advanced.database.generateId: false`. This disables Better Auth's internal ID generation and lets MongoDB assign ObjectIds automatically [source](https://github.com/better-auth/better-auth/pull/3723). Example: ```prisma model User { id String @id @map("_id") @db.ObjectId // ...other fields } ``` ```typescript export const auth = betterAuth({ database: { db: db, }, advanced: { database: { generateId: false, }, }, // ...other config }); ``` After making these changes, MongoDB will generate ObjectIds for new documents, and Prisma will treat them correctly. If you upgrade Better Auth in the future, check if the generator adds `@db.ObjectId` automatically—current versions do not [source](https://github.com/better-auth/better-auth/pull/3021). <!-- 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/34143fa8-f3cd-4133-9e2d-d98a6a7c003a?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/34143fa8-f3cd-4133-9e2d-d98a6a7c003a?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/34143fa8-f3cd-4133-9e2d-d98a6a7c003a?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/34143fa8-f3cd-4133-9e2d-d98a6a7c003a?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/34143fa8-f3cd-4133-9e2d-d98a6a7c003a?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/34143fa8-f3cd-4133-9e2d-d98a6a7c003a?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/34143fa8-f3cd-4133-9e2d-d98a6a7c003a?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/4106)
Author
Owner

@bayromest commented on GitHub (Aug 20, 2025):

Solution:

1- in auth.ts add the following:

advanced: {
    database: {
      generateId: false,
    },
  },

2- Change all the ID's of the models generated by better-auth (Session, Account, Verification, User) from @id @map("_id") to @id @default(auto()) @map("_id") @db.ObjectId

@bayromest commented on GitHub (Aug 20, 2025): Solution: 1- in auth.ts add the following: ```ts advanced: { database: { generateId: false, }, }, ``` 2- Change all the ID's of the models generated by better-auth (Session, Account, Verification, User) from ```@id @map("_id")``` to ```@id @default(auto()) @map("_id") @db.ObjectId```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1746