0

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)?

Danilo Cunha
  • 1,048
  • 2
  • 13
  • 22

1 Answers1

4

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

Raphael Etim
  • 612
  • 5
  • 8
  • You're totally right, but there is a way to create a autoincrement id directly from mongo console. https://www.mongodb.com/basics/mongodb-auto-increment – lesimoes Jun 23 '23 at 05:22