Prisma ORM Skill
You are a Prisma ORM expert for TypeScript applications.
Critical Rules
- Run
prisma generate after schema changes — the client is code-generated, not runtime
- Use
migrate dev in development — creates migration files and applies them
- Use
migrate deploy in production — applies pending migrations without creating new ones
- Never use
db push in production — it doesn't create migration files and can lose data
- Name relations explicitly — when a model has multiple relations to the same target
- Add
@@index for query performance — Prisma doesn't auto-create indexes on FKs
- Use transactions for multi-step operations —
prisma.$transaction() for consistency
Schema Design
model User {
id String @id @default(uuid())
email String @unique
name String?
posts Post[]
profile Profile?
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@map("users")
@@index([email])
}
model Post {
id String @id @default(uuid())
title String
content String?
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
authorId String @map("author_id")
@@map("posts")
@@index([authorId])
}
Key attributes: @id, @unique, @default(), @map(), @relation(), @@map(), @@index(), @@unique(). Read reference/schema-patterns.md for all types and patterns.
Relations
| Type |
Schema Pattern |
| One-to-One |
FK with @unique on one side, optional ? field on other |
| One-to-Many |
FK on "many" side, array [] on "one" side |
| Many-to-Many (implicit) |
Array [] on both sides, Prisma creates junction table |
| Many-to-Many (explicit) |
Junction model with two @relation fields |
| Self-relation |
Model references itself (e.g., parent/children) |
Client Usage
// Create with relation
const user = await prisma.user.create({
data: { email: 'a@b.com', name: 'Alice', posts: { create: { title: 'Hello' } } },
include: { posts: true },
});
// Query with filtering
const posts = await prisma.post.findMany({
where: { author: { email: { contains: '@company.com' } } },
orderBy: { createdAt: 'desc' },
take: 20,
skip: 0,
select: { id: true, title: true, author: { select: { name: true } } },
});
// Cursor pagination (preferred over offset)
const next = await prisma.post.findMany({
cursor: { id: lastId },
skip: 1,
take: 20,
orderBy: { id: 'asc' },
});
Read reference/client-patterns.md for CRUD recipes, nested writes, transactions, and error handling.
Migrations
# Development: create and apply migration
npx prisma migrate dev --name add_user_table
# Production: apply pending migrations
npx prisma migrate deploy
# Reset database (development only)
npx prisma migrate reset
# Generate client after schema change
npx prisma generate
# Browse database
npx prisma studio
Read reference/migration-workflow.md for CI/CD integration, handling drift, and rollback strategies.
Advanced
- Raw queries —
prisma.$queryRaw for complex SQL not supported by client
- Transactions —
prisma.$transaction([...]) for sequential, or prisma.$transaction(async (tx) => {...}) for interactive
- Middleware —
prisma.$use() for logging, soft delete, audit trails
- Connection pooling — use
pgbouncer=true in connection string for serverless
Anti-Patterns
- Don't use
db push in prod — no migration history, can drop columns
- Don't skip
@@index — Prisma doesn't auto-index foreign keys
- Don't fetch all fields — use
select to reduce payload
- Don't ignore N+1 — use
include to eager-load relations
- Don't edit migration files — create new migrations to fix issues
- Don't use
prisma migrate dev in CI — use prisma migrate deploy
Related
reference/schema-patterns.md — Model patterns, field types, attributes, enums, multi-file schemas
reference/client-patterns.md — CRUD recipes, filtering, transactions, raw SQL, error handling
reference/migration-workflow.md — Dev vs prod workflow, seeding, CI/CD, rollback
1---2name: prisma-orm3description: Prisma ORM expertise for schema design, migrations, client usage, and deployment. Use when writing Prisma schemas, creating migrations, querying with Prisma Client, defining relations, or deploying database changes. Triggers on "prisma", "prisma schema", "prisma migrate", "prisma client", "prisma relation", "@@map", "@@index", "prisma generate", "prisma studio", "prisma deploy".4license: MIT5---67# Prisma ORM Skill89You are a Prisma ORM expert for TypeScript applications.1011## Critical Rules1213- **Run `prisma generate` after schema changes** — the client is code-generated, not runtime14- **Use `migrate dev` in development** — creates migration files and applies them15- **Use `migrate deploy` in production** — applies pending migrations without creating new ones16- **Never use `db push` in production** — it doesn't create migration files and can lose data17- **Name relations explicitly** — when a model has multiple relations to the same target18- **Add `@@index` for query performance** — Prisma doesn't auto-create indexes on FKs19- **Use transactions for multi-step operations** — `prisma.$transaction()` for consistency2021## Schema Design2223```prisma24model User {25 id String @id @default(uuid())26 email String @unique27 name String?28 posts Post[]29 profile Profile?30 createdAt DateTime @default(now()) @map("created_at")31 updatedAt DateTime @updatedAt @map("updated_at")3233 @@map("users")34 @@index([email])35}3637model Post {38 id String @id @default(uuid())39 title String40 content String?41 author User @relation(fields: [authorId], references: [id], onDelete: Cascade)42 authorId String @map("author_id")4344 @@map("posts")45 @@index([authorId])46}47```4849Key attributes: `@id`, `@unique`, `@default()`, `@map()`, `@relation()`, `@@map()`, `@@index()`, `@@unique()`. Read `reference/schema-patterns.md` for all types and patterns.5051## Relations5253| Type | Schema Pattern |54|------|---------------|55| One-to-One | FK with `@unique` on one side, optional `?` field on other |56| One-to-Many | FK on "many" side, array `[]` on "one" side |57| Many-to-Many (implicit) | Array `[]` on both sides, Prisma creates junction table |58| Many-to-Many (explicit) | Junction model with two `@relation` fields |59| Self-relation | Model references itself (e.g., `parent`/`children`) |6061## Client Usage6263```typescript64// Create with relation65const user = await prisma.user.create({66 data: { email: 'a@b.com', name: 'Alice', posts: { create: { title: 'Hello' } } },67 include: { posts: true },68});6970// Query with filtering71const posts = await prisma.post.findMany({72 where: { author: { email: { contains: '@company.com' } } },73 orderBy: { createdAt: 'desc' },74 take: 20,75 skip: 0,76 select: { id: true, title: true, author: { select: { name: true } } },77});7879// Cursor pagination (preferred over offset)80const next = await prisma.post.findMany({81 cursor: { id: lastId },82 skip: 1,83 take: 20,84 orderBy: { id: 'asc' },85});86```8788Read `reference/client-patterns.md` for CRUD recipes, nested writes, transactions, and error handling.8990## Migrations9192```bash93# Development: create and apply migration94npx prisma migrate dev --name add_user_table9596# Production: apply pending migrations97npx prisma migrate deploy9899# Reset database (development only)100npx prisma migrate reset101102# Generate client after schema change103npx prisma generate104105# Browse database106npx prisma studio107```108109Read `reference/migration-workflow.md` for CI/CD integration, handling drift, and rollback strategies.110111## Advanced112113- **Raw queries** — `prisma.$queryRaw` for complex SQL not supported by client114- **Transactions** — `prisma.$transaction([...])` for sequential, or `prisma.$transaction(async (tx) => {...})` for interactive115- **Middleware** — `prisma.$use()` for logging, soft delete, audit trails116- **Connection pooling** — use `pgbouncer=true` in connection string for serverless117118## Anti-Patterns119120- **Don't use `db push` in prod** — no migration history, can drop columns121- **Don't skip `@@index`** — Prisma doesn't auto-index foreign keys122- **Don't fetch all fields** — use `select` to reduce payload123- **Don't ignore N+1** — use `include` to eager-load relations124- **Don't edit migration files** — create new migrations to fix issues125- **Don't use `prisma migrate dev` in CI** — use `prisma migrate deploy`126127## Related128129- `reference/schema-patterns.md` — Model patterns, field types, attributes, enums, multi-file schemas130- `reference/client-patterns.md` — CRUD recipes, filtering, transactions, raw SQL, error handling131- `reference/migration-workflow.md` — Dev vs prod workflow, seeding, CI/CD, rollback