Technical questionAccepted answer
How to make id autoincrement in schema.prisma?
lian. lun
Jun 11
0 views1
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")
}
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.
1 answer
Accepted answer · original discussion
Nurul Sundarani
PermalinkJun 13
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")
}
0 question comments
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.