10

I am writing a postgreSQL database. The ID must be auto-incrementing.

model User {
 id            String    @id @default(cuid())
 name          String?
 email         String?   @unique
 emailVerified DateTime? @map("email_verified")
 image         String?
 createdAt     DateTime  @default(now()) @map(name: "created_at")
 updatedAt     DateTime  @updatedAt @map(name: "updated_at")
 posts         Post[]
 accounts      Account[]
 sessions      Session[]

 @@map(name: "users")
}

Result

If you write the ID manually, you can enter any value as in the first entry, but autocomplete generates the code as in the second entry. I got the code from the site Vercel.

lian. lun
  • 115
  • 1
  • 1
  • 9

1 Answers1

12

You need to use autoincrement function.

Here's how it would look in User model:

model User {
 id            Int       @id @default(autoincrement())
 name          String?
 email         String?   @unique
 emailVerified DateTime? @map("email_verified")
 image         String?
 createdAt     DateTime  @default(now()) @map(name: "created_at")
 updatedAt     DateTime  @updatedAt @map(name: "updated_at")
 posts         Post[]
 accounts      Account[]
 sessions      Session[]

 @@map(name: "users")
}
Nurul Sundarani
  • 5,550
  • 3
  • 23
  • 52