Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make id autoincrement in schema.prisma?

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.

like image 202
lian. lun Avatar asked Dec 04 '25 09:12

lian. lun


1 Answers

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")
}
like image 124
Nurul Sundarani Avatar answered Dec 06 '25 01:12

Nurul Sundarani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!