better-auth+prisma+mongodb combo literally doesn't work

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 },
include: {
gigs: true,
orders: true,
},
});
}
export async function GetUserById(id: string): Promise<User | null> {
return prisma.user.findUnique({
where: { id },
include: {
gigs: true,
orders: true,
},
});
}
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".
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)
No description
Solution:
I found this solution: Solution: 1- in auth.ts add the following:...
Jump to solution
2 Replies
bayrem🪽
bayrem🪽OP2w ago
GitHub
better auth+prisma+mongodb combo literally doesn't work · Issue #4...
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&#39;t hav...
Solution
bayrem🪽
bayrem🪽2w ago
I found this solution: Solution: 1- in auth.ts add the following:
advanced: {
database: {
generateId: false,
},
},
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")
@id @map("_id")
to
@id @default(auto()) @map("_id") @db.ObjectId
@id @default(auto()) @map("_id") @db.ObjectId

Did you find this page helpful?