model User { id Int @id @default(autoincrement()) email String @unique name String? posts Post[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt }
model Post { id Int @id @default(autoincrement()) title String content String? published Boolean @default(false) author User @relation(fields: [authorId], references: [id]) authorId Int createdAt DateTime @default(now()) updatedAt DateTime @updatedAt }
datasource db { provider = "postgresql" url = env("DATABASE_URL") }
创建 prisma/models/user.prisma(用户模型):
1 2 3 4 5 6 7 8 9 10 11 12 13
// prisma/models/user.prisma model User { id Int @id @default(autoincrement()) email String @unique name String? posts Post[] comments Comment[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@index([email]) @@map("users") }
创建 prisma/models/post.prisma(文章模型):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// prisma/models/post.prisma model Post { id Int @id @default(autoincrement()) title String content String? published Boolean @default(false) author User @relation(fields: [authorId], references: [id]) authorId Int comments Comment[] tags Tag[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@index([authorId]) @@map("posts") }
创建 prisma/models/comment.prisma(评论模型):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// prisma/models/comment.prisma model Comment { id Int @id @default(autoincrement()) content String post Post @relation(fields: [postId], references: [id]) postId Int author User @relation(fields: [authorId], references: [id]) authorId Int createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@index([postId]) @@index([authorId]) @@map("comments") }
创建 prisma/models/tag.prisma(标签模型):
1 2 3 4 5 6 7 8 9 10 11
// prisma/models/tag.prisma model Tag { id Int @id @default(autoincrement()) name String @unique description String? posts Post[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@map("tags") }