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,
    },
  });
}


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)
2CEQFY0.png
Solution
I found this solution:

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
Was this page helpful?