mongodbprisma

The right way to create an auto increment ID on Prisma using mongodb


I'm trying to create an auto increment ID with Prisma, but the autoincrement() function doesn't exist with mongodb as data-source.

model User {
  id        String   @id @default(auto()) @map("_id") @db.ObjectId
  sequence  Int      @unique @default(autoincrement()) // This doesn't work with mongodb
  email     String   @unique
  password  String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

I know I can make a query to check the last sequence but this seens to be too expensive to do.

There is a better way to this?

Like in prisma.user.create() there is a way to get the last sequence field (like we do with mongoose)?


Solution

  • MongoDB connector for prisma doesn't support autoincrement(). autoincrement() is only supported by the relational database connectors. See the Prisma docs that explains this. You can also view a previous discussion related to this topic here