[GH-ISSUE #5770] PrismaClientValidationError when creating user with MongoDB and Better Auth #27667

Closed
opened 2026-04-17 18:48:32 -05:00 by GiteaMirror · 7 comments
Owner

Originally created by @Winniper on GitHub (Nov 5, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/5770

Environment:
Better Auth version: 1.3.34
Prisma version: 6.18.0
Database: MongoDB Atlas
Next.js version: 16.0.0 (Turbopack)

Description:
I'm encountering a PrismaClientValidationError when trying to create a user using Better Auth's email/password authentication with MongoDB. The signup request fails with a 422 status code.

Error Message:
{ "code": "FAILED_TO_CREATE_USER", "message": "Failed to create user", "details": { "name": "PrismaClientValidationError", "clientVersion": "6.18.0" } }

Current Configuration:

Prisma Schema:

generator client {
provider = "prisma-client-js"
output = "../generated/prisma/client"
}

datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}

model user {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String
email String @unique
emailVerified Boolean @default(false)
image String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sessions session[]
accounts account[]
}

model session {
id String @id @default(auto()) @map("_id") @db.ObjectId
expiresAt DateTime
token String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
ipAddress String?
userAgent String?
userId String @db.ObjectId
user user @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model account {
id String @id @default(auto()) @map("_id") @db.ObjectId
accountId String
providerId String
userId String @db.ObjectId
user user @relation(fields: [userId], references: [id], onDelete: Cascade)
accessToken String?
refreshToken String?
idToken String?
accessTokenExpiresAt DateTime?
refreshTokenExpiresAt DateTime?
scope String?
password String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

@@unique([providerId, accountId])
}

model verification {
id String @id @default(auto()) @map("_id") @db.ObjectId
identifier String
value String
expiresAt DateTime
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

@@unique([identifier, value])
}

What I've Tried:

  1. Added @db.ObjectId to all ID fields and foreign keys
  2. Added @default(auto()) to all ID fields
  3. Configured generateId: false in Better Auth advanced settings
  4. Added unique constraints @@unique([providerId, accountId]) on account model
  5. Regenerated Prisma client multiple times
  6. Cleared Next.js cache and restarted dev server

Expected Behavior:
User should be created successfully in MongoDB when signing up with email and password.

Actual Behavior:
Getting PrismaClientValidationError indicating that the data Better Auth is trying to insert doesn't match the Prisma schema validation requirements

Originally created by @Winniper on GitHub (Nov 5, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/5770 **Environment:** Better Auth version: 1.3.34 Prisma version: 6.18.0 Database: MongoDB Atlas Next.js version: 16.0.0 (Turbopack) **Description:** I'm encountering a PrismaClientValidationError when trying to create a user using Better Auth's email/password authentication with MongoDB. The signup request fails with a 422 status code. **Error Message:** `{ "code": "FAILED_TO_CREATE_USER", "message": "Failed to create user", "details": { "name": "PrismaClientValidationError", "clientVersion": "6.18.0" } }` **Current Configuration:** **Prisma Schema:** generator client { provider = "prisma-client-js" output = "../generated/prisma/client" } datasource db { provider = "mongodb" url = env("DATABASE_URL") } model user { id String @id @default(auto()) @map("_id") @db.ObjectId name String email String @unique emailVerified Boolean @default(false) image String? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt sessions session[] accounts account[] } model session { id String @id @default(auto()) @map("_id") @db.ObjectId expiresAt DateTime token String @unique createdAt DateTime @default(now()) updatedAt DateTime @updatedAt ipAddress String? userAgent String? userId String @db.ObjectId user user @relation(fields: [userId], references: [id], onDelete: Cascade) } model account { id String @id @default(auto()) @map("_id") @db.ObjectId accountId String providerId String userId String @db.ObjectId user user @relation(fields: [userId], references: [id], onDelete: Cascade) accessToken String? refreshToken String? idToken String? accessTokenExpiresAt DateTime? refreshTokenExpiresAt DateTime? scope String? password String? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@unique([providerId, accountId]) } model verification { id String @id @default(auto()) @map("_id") @db.ObjectId identifier String value String expiresAt DateTime createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@unique([identifier, value]) } **What I've Tried:** 1. Added @db.ObjectId to all ID fields and foreign keys 2. Added @default(auto()) to all ID fields 3. Configured generateId: false in Better Auth advanced settings 4. Added unique constraints @@unique([providerId, accountId]) on account model 5. Regenerated Prisma client multiple times 6. Cleared Next.js cache and restarted dev server **Expected Behavior:** User should be created successfully in MongoDB when signing up with email and password. **Actual Behavior:** Getting PrismaClientValidationError indicating that the data Better Auth is trying to insert doesn't match the Prisma schema validation requirements
GiteaMirror added the lockedbug labels 2026-04-17 18:48:32 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Nov 5, 2025):

(tag @better-auth-agent if you would like an answer from the Agent)

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3489687321 --> @better-auth-agent[bot] commented on GitHub (Nov 5, 2025): (tag @better-auth-agent if you would like an answer from the Agent) <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@dosubot[bot] commented on GitHub (Nov 5, 2025):

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

Your Prisma schema and config look correct for MongoDB, but this error is almost always caused by a mismatch between how IDs are generated and what Prisma expects. With MongoDB, Prisma requires ObjectId values for all primary and foreign keys. If Better Auth tries to insert a string ID (its default), you'll get a PrismaClientValidationError.

You've already set generateId: false in your Better Auth config, which is correct—this tells Better Auth to let MongoDB generate ObjectIds automatically. Your schema also uses @db.ObjectId and @default(auto()), which is required. Make sure you have regenerated your Prisma client after these changes.

If you're still seeing the error, double-check these common pitfalls:

  • All required fields in your Prisma schema (like name) must be provided during signup. If you don't collect a name, make the field optional or provide a placeholder value. This is a known cause of user creation failures with Better Auth's email/password flow example.
  • The emailVerified field must be a Boolean, not a DateTime or nullable type. Better Auth expects to write a boolean value here example.
  • If you have any additional required fields in your schema, make sure they are included in the data sent to Prisma, or make them optional example.
  • If you have custom plugins or code that modifies the user creation payload, ensure they do not set id: null or otherwise interfere with ID handling.

Recent changes in Better Auth (as of late October 2025) fixed several MongoDB ID handling bugs, so if you are not on the latest version, consider upgrading see PR #5686.

In summary: your config and schema are correct, but check for missing required fields, field type mismatches, or custom code that might interfere with the user creation payload. If all else fails, upgrading Better Auth may resolve the issue if it's related to recent MongoDB adapter fixes.

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:3489699362 --> @dosubot[bot] commented on GitHub (Nov 5, 2025): <!-- Greeting --> Hi @Winniper! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> Your Prisma schema and config look correct for MongoDB, but this error is almost always caused by a mismatch between how IDs are generated and what Prisma expects. With MongoDB, Prisma requires ObjectId values for all primary and foreign keys. If Better Auth tries to insert a string ID (its default), you'll get a PrismaClientValidationError. You've already set `generateId: false` in your Better Auth config, which is correct—this tells Better Auth to let MongoDB generate ObjectIds automatically. Your schema also uses `@db.ObjectId` and `@default(auto())`, which is required. Make sure you have regenerated your Prisma client after these changes. If you're still seeing the error, double-check these common pitfalls: - All required fields in your Prisma schema (like `name`) must be provided during signup. If you don't collect a name, make the field optional or provide a placeholder value. This is a known cause of user creation failures with Better Auth's email/password flow [example](https://github.com/better-auth/better-auth/issues/3394). - The `emailVerified` field must be a Boolean, not a DateTime or nullable type. Better Auth expects to write a boolean value here [example](https://github.com/better-auth/better-auth/issues/3469). - If you have any additional required fields in your schema, make sure they are included in the data sent to Prisma, or make them optional [example](https://github.com/better-auth/better-auth/issues/371). - If you have custom plugins or code that modifies the user creation payload, ensure they do not set `id: null` or otherwise interfere with ID handling. Recent changes in Better Auth (as of late October 2025) fixed several MongoDB ID handling bugs, so if you are not on the latest version, consider upgrading [see PR #5686](https://github.com/better-auth/better-auth/pull/5686). In summary: your config and schema are correct, but check for missing required fields, field type mismatches, or custom code that might interfere with the user creation payload. If all else fails, upgrading Better Auth may resolve the issue if it's related to recent MongoDB adapter fixes. <!-- 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/47b26146-8417-41f4-9962-a339573876df?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/47b26146-8417-41f4-9962-a339573876df?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/47b26146-8417-41f4-9962-a339573876df?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/47b26146-8417-41f4-9962-a339573876df?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/47b26146-8417-41f4-9962-a339573876df?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/47b26146-8417-41f4-9962-a339573876df?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/47b26146-8417-41f4-9962-a339573876df?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/5770)
Author
Owner

@Winniper commented on GitHub (Nov 5, 2025):

@dosu Thank you for the detailed response.
I've verified all the points you mentioned:

However, I'm still getting the error. Looking at the server logs, I can see the exact issue:
`→ 9429 return await db[model].create({
data: {
name: "testuser1",
email: "testmail1@gamil.dev",
emailVerified: false,
createdAt: new Date("2025-11-05T07:00:09.125Z"),
updatedAt: new Date("2025-11-05T07:00:09.125Z"),
+ password: String // It's passing the TYPE, not the actual password value
},
select: undefined
})

Argument password is missing.`

Better Auth is trying to insert [password: String](the type) instead of the actual hashed password value into the user model. This appears to be the exact bug you mentioned that was fixed in PR #5686.

I'm currently on better-auth@1.3.34.

<!-- gh-comment-id:3489743537 --> @Winniper commented on GitHub (Nov 5, 2025): @dosu Thank you for the detailed response. I've verified all the points you mentioned: However, I'm still getting the error. Looking at the server logs, I can see the exact issue: `→ 9429 return await db[model].create({ data: { name: "testuser1", email: "testmail1@gamil.dev", emailVerified: false, createdAt: new Date("2025-11-05T07:00:09.125Z"), updatedAt: new Date("2025-11-05T07:00:09.125Z"), + password: String // ❌ It's passing the TYPE, not the actual password value }, select: undefined }) Argument `password` is missing.` Better Auth is trying to insert [password: String](the type) instead of the actual hashed password value into the user model. This appears to be the exact bug you mentioned that was fixed in PR #5686. I'm currently on better-auth@1.3.34.
Author
Owner

@dosubot[bot] commented on GitHub (Nov 5, 2025):

You're right—this is a bug in better-auth@1.3.34 where the password field is assigned the JavaScript type String instead of the actual hashed password value during user creation with MongoDB and Prisma, which causes the PrismaClientValidationError you’re seeing. This bug was fixed in a later release after PR #5686, which improved how user creation payloads are constructed and validated for MongoDB/Prisma adapters.

To resolve this, you’ll need to upgrade to a version of Better Auth released after PR #5686 (merged 2025-10-31). There isn’t a reliable workaround for 1.3.34, since the bug is in the adapter’s internal payload construction logic. If you can’t upgrade immediately, you’d need to patch the adapter locally to ensure the password field is set to the actual hashed value, but upgrading is the recommended and most robust fix.

Reference: PR #5686 - fix(adapters): mongodb id issue

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:3489752786 --> @dosubot[bot] commented on GitHub (Nov 5, 2025): <!-- Answer --> You're right—this is a bug in better-auth@1.3.34 where the password field is assigned the JavaScript type String instead of the actual hashed password value during user creation with MongoDB and Prisma, which causes the PrismaClientValidationError you’re seeing. This bug was fixed in a later release after PR #5686, which improved how user creation payloads are constructed and validated for MongoDB/Prisma adapters. To resolve this, you’ll need to upgrade to a version of Better Auth released after PR #5686 (merged 2025-10-31). There isn’t a reliable workaround for 1.3.34, since the bug is in the adapter’s internal payload construction logic. If you can’t upgrade immediately, you’d need to patch the adapter locally to ensure the password field is set to the actual hashed value, but upgrading is the recommended and most robust fix. Reference: [PR #5686 - fix(adapters): mongodb id issue](https://github.com/better-auth/better-auth/pull/5686) <!-- 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/35bfec4c-e5eb-40eb-8d72-a0424f3236e2?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/35bfec4c-e5eb-40eb-8d72-a0424f3236e2?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/35bfec4c-e5eb-40eb-8d72-a0424f3236e2?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/35bfec4c-e5eb-40eb-8d72-a0424f3236e2?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/35bfec4c-e5eb-40eb-8d72-a0424f3236e2?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/35bfec4c-e5eb-40eb-8d72-a0424f3236e2?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/35bfec4c-e5eb-40eb-8d72-a0424f3236e2?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/5770)
Author
Owner

@Dayo-001 commented on GitHub (Nov 18, 2025):

@dosu, I am currently experiencing this same issue of PrismaClientValidationError, where the password input is passed as a type, instead of the actual hashed value. I am working with a Postgresql database. I was wondering if you could attach a link to the documentation or page where the latest version of Better Auth is? I can't locate it.

<!-- gh-comment-id:3548173232 --> @Dayo-001 commented on GitHub (Nov 18, 2025): @dosu, I am currently experiencing this same issue of PrismaClientValidationError, where the password input is passed as a type, instead of the actual hashed value. I am working with a Postgresql database. I was wondering if you could attach a link to the documentation or page where the latest version of Better Auth is? I can't locate it.
Author
Owner

@dosubot[bot] commented on GitHub (Feb 18, 2026):

Hi, @Winniper. I'm Dosu, and I'm helping the better-auth team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You reported a PrismaClientValidationError in Better Auth 1.3.34 with Prisma 6.18.0 and MongoDB due to the password field being set incorrectly.
  • This was identified as a known bug fixed after PR #5686, merged on 2025-10-31.
  • You verified your schema and config but still encountered the error because of this bug.
  • Another user reported a similar issue with PostgreSQL and requested updated documentation.
  • The resolution is to upgrade Better Auth to a version released after the fix in PR #5686.

Next Steps:

  • Please confirm if this issue is still relevant with the latest version of Better Auth; if so, you can keep the discussion open by commenting here.
  • Otherwise, I will automatically close this issue in 7 days.

Thank you for your understanding and contribution!

<!-- gh-comment-id:3921725058 --> @dosubot[bot] commented on GitHub (Feb 18, 2026): Hi, @Winniper. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary:** - You reported a PrismaClientValidationError in Better Auth 1.3.34 with Prisma 6.18.0 and MongoDB due to the password field being set incorrectly. - This was identified as a known bug fixed after PR #5686, merged on 2025-10-31. - You verified your schema and config but still encountered the error because of this bug. - Another user reported a similar issue with PostgreSQL and requested updated documentation. - The resolution is to upgrade Better Auth to a version released after the fix in PR #5686. **Next Steps:** - Please confirm if this issue is still relevant with the latest version of Better Auth; if so, you can keep the discussion open by commenting here. - Otherwise, I will automatically close this issue in 7 days. Thank you for your understanding and contribution!
Author
Owner

@Bekacru commented on GitHub (Feb 18, 2026):

This isn't a bug. Better Auth doesnt store password in the user table

<!-- gh-comment-id:3923557512 --> @Bekacru commented on GitHub (Feb 18, 2026): This isn't a bug. Better Auth doesnt store password in the user table
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#27667