我正在编写一个 postgreSQL 数据库。 ID 必须自动递增。
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")
}
如果您手动输入 ID,您可以像在第一个条目中那样输入任何值,但自动完成会像在第二个条目中那样生成代码。我从网站 Vercel 获得了代码.
最佳答案
您需要使用 autoincrement功能。
这是它在 User
模型中的样子:
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")
}
关于postgresql - 如何在 schema.prisma 中使 id 自增?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72583324/