Prisma Cheat Sheet
Quick reference for Prisma ORM — schema definition, model fields, relations, CRUD operations, filtering, sorting, migrations, and advanced queries. All essential patterns in one page.
Schema Definition
Model Fields
Relations
CRUD Operations
Filtering & Sorting
Migrations
Advanced Queries
Schema Definition
| datasource db { provider = "postgresql" url = env("DATABASE_URL") } | Database connection config |
| generator client { provider = "prisma-client-js" } | Prisma Client generator |
| generator client { previewFeatures = ["fullTextSearch"] } | Enable preview features |
| model User { id Int @id @default(autoincrement()) } | Model with auto-increment primary key |
| model User { id String @id @default(uuid()) } | Model with UUID primary key |
| model User { id String @id @default(cuid()) } | Model with CUID primary key |
| @@map("users") | Map model to custom table name |
| @@index([email, name]) | Composite index |
| @@unique([email, tenantId]) | Composite unique constraint |
Model Fields
| name String | Required string field |
| bio String? | Optional field (nullable) |
| role Role @default(USER) | Field with default enum value |
| createdAt DateTime @default(now()) | Auto-set creation timestamp |
| updatedAt DateTime @updatedAt | Auto-update timestamp |
| email String @unique | Unique constraint |
| age Int @db.SmallInt | Native database type mapping |
| tags String[] | Array/list field (PostgreSQL) |
| enum Role { USER ADMIN MODERATOR } | Enum type definition |
| data Json | JSON field type |
Relations
| posts Post[] | One-to-many relation (parent side) |
| author User @relation(fields: [authorId], references: [id]) | Many-to-one relation (child side) |
| authorId Int | Foreign key field |
| profile Profile? @relation(...) | Optional one-to-one relation |
| @relation(onDelete: Cascade) | Cascade delete on relation |
| @relation(onDelete: SetNull) | Set null on parent delete |
| categories Category[] @relation("PostCategories") | Many-to-many (implicit join table) |
| @relation("PostCategories") | Named relation for disambiguation |
| @relation(fields: [authorId], references: [id], map: "fk_author") | Custom foreign key name |
CRUD Operations
| prisma.user.create({ data: { name: "Alice", email: "[email protected]" } }) | Create a record |
| prisma.user.createMany({ data: [...], skipDuplicates: true }) | Bulk create, skip duplicates |
| prisma.user.findUnique({ where: { id: 1 } }) | Find by unique field |
| prisma.user.findFirst({ where: { name: "Alice" } }) | Find first matching record |
| prisma.user.findMany({ skip: 0, take: 10 }) | Paginated list |
| prisma.user.update({ where: { id: 1 }, data: { name: "Bob" } }) | Update a record |
| prisma.user.updateMany({ where: { role: "USER" }, data: { active: true } }) | Bulk update |
| prisma.user.upsert({ where: { email }, create: {...}, update: {...} }) | Create or update |
| prisma.user.delete({ where: { id: 1 } }) | Delete a record |
| prisma.user.count({ where: { active: true } }) | Count matching records |
Filtering & Sorting
| where: { name: { contains: "ali", mode: "insensitive" } } | Case-insensitive contains |
| where: { age: { gte: 18, lte: 65 } } | Range filter (>=, <=) |
| where: { email: { startsWith: "admin" } } | Starts with filter |
| where: { OR: [{ name: "A" }, { name: "B" }] } | OR condition |
| where: { AND: [{ active: true }, { role: "ADMIN" }] } | AND condition |
| where: { NOT: { email: { contains: "test" } } } | NOT condition |
| where: { tags: { has: "featured" } } | Array contains value |
| orderBy: { createdAt: "desc" } | Sort descending |
| orderBy: [{ role: "asc" }, { name: "asc" }] | Multi-column sort |
Migrations
| npx prisma migrate dev --name init | Create and apply migration |
| npx prisma migrate deploy | Apply pending migrations (production) |
| npx prisma migrate reset | Reset database and re-apply all migrations |
| npx prisma db push | Push schema changes without migration (prototyping) |
| npx prisma db pull | Introspect existing database into schema |
| npx prisma generate | Regenerate Prisma Client |
| npx prisma studio | Open visual database browser |
| npx prisma db seed | Run seed script |
| npx prisma format | Format schema file |
Advanced Queries
| include: { posts: true } | Eager load relation |
| include: { posts: { where: { published: true }, take: 5 } } | Filtered relation loading |
| select: { id: true, name: true } | Select specific fields only |
| prisma.$transaction([query1, query2]) | Batch transaction (sequential) |
| prisma.$transaction(async (tx) => { ... }) | Interactive transaction |
| prisma.user.aggregate({ _avg: { age: true }, _count: true }) | Aggregation query |
| prisma.user.groupBy({ by: ["role"], _count: true }) | Group by with count |
| prisma.$queryRaw`SELECT * FROM users WHERE id = ${id}` | Raw SQL query (parameterized) |
| prisma.user.findMany({ cursor: { id: 10 }, take: 20 }) | Cursor-based pagination |
Step-by-Step Guide
Read Guide →