| name | prisma-schema-design |
| description | null |
Prisma Schema Design
Category: prisma · Status: 🟢 Active
When to use
Khi thiết kế hoặc sửa Prisma schema: model, relation, field type, naming.
Steps
- Model = danh từ số ít, PascalCase (
User); field camelCase; map sang DB bằng @@map/@map nếu cần snake_case.
- Chọn type sát domain:
String, Int, Decimal (tiền), DateTime, enum thay cho chuỗi tự do.
- Khai báo relation 2 chiều, dùng
@relation với fields/references rõ ràng.
- Mặc định hợp lý:
@id @default(cuid()), createdAt @default(now()), updatedAt @updatedAt.
- Nullable chỉ khi thật sự optional; còn lại để bắt buộc + default.
Template
model Post {
id String @id @default(cuid())
title String
status PostStatus @default(DRAFT)
authorId String
author User @relation(fields: [authorId], references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@map("posts")
}
enum PostStatus { DRAFT PUBLISHED }
Example
Good: type đúng domain, relation 2 chiều, default chuẩn, enum thay magic string.
Avoid: lạm dụng String cho mọi thứ, relation 1 chiều mơ hồ, thiếu createdAt/updatedAt.
Checklist