Prisma Cheat Sheet: Schema, Relations, Migrate and Client Queries
Checked against Prisma ORM Prisma ORM 7.10.1 docs; schema examples validated with Prisma CLI 6.19.3 on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for TypeScript developers who use Prisma ORM day to day and keep looking up the same schema attributes and CLI flags. It follows the Prisma 7 documentation (latest release 7.10.1, 19 Oct 2026). The schema block in the example was validated locally with the Prisma 6.19.3 CLI that this site still runs, and the CLI and Client rows are marked as checked in the docs only.
When to use what
Use prisma migrate dev whenever the schema change has to reach another database later. It writes a SQL file into prisma/migrations, and migrate deploy replays those files in CI or production without ever resetting anything. Reach for db push only for prototypes and throwaway databases: it syncs the schema with no history, so there is nothing to deploy afterwards.
Pick include when you want the whole related record and select when you care about payload size. On list endpoints I default to select, because a wide include on a big table quietly ships columns nobody reads. The Prisma performance tips cover the N+1 traps.
For transactions, the array form of $transaction is enough when the queries don't depend on each other. Use the interactive form when step two needs the result of step one, and keep the callback short, because the default timeout is 5 seconds. If you are moving between raw SQL and Prisma, the SQL to Prisma converter drafts models from CREATE TABLE statements and Prisma to SQL goes the other way. The Prisma 7 REST API post shows the setup end to end.
Scalar lists like String[] need PostgreSQL or CockroachDB. @updatedAt is set by Prisma, not by the database.
String Boolean Int BigInt Float Decimal DateTime Json BytesThe built-in scalar types you can give a field
name String?Question mark makes the column nullable
id Int @id @default(autoincrement())Integer primary key the database increments
id String @id @default(cuid())String primary key generated by Prisma; uuid(), ulid() and nanoid() also exist
email String @uniqueAdds a unique constraint on one column
createdAt DateTime @default(now())Fills the timestamp when the row is created
updatedAt DateTime @updatedAtPrisma refreshes the timestamp on every update
nickname String @map("nick_name")Keep a camelCase field name while the column is snake_case
bio String @db.TextPick a native column type instead of the default mapping
The side with @relation(fields: ..., references: ...) holds the foreign key. Explicit many-to-many syntax is from https://www.prisma.io/docs/orm/v7/prisma-schema/data-model/relations/many-to-many-relations.
user User @relation(fields: [userId], references: [id])
userId Int @uniqueOne-to-one: the @unique on the foreign key is what stops it being one-to-many
profile Profile?Back-relation field on the other side of a 1-1; no column is created
posts Post[]List side of a one-to-many; the child model holds authorId
categories Category[] / posts Post[]Implicit many-to-many: list fields on both sides and Prisma manages the join table
model CategoriesOnPosts { ... @@id([postId, categoryId]) }Explicit many-to-many: your own join model, useful when the link needs extra columns
@relation(fields: [userId], references: [id], onDelete: Cascade)Delete the child rows when the parent is deleted
enum Role { USER ADMIN }Declare an enum (one value per line in a real file)
role Role @default(USER)Use the enum as a field type with a default
@@index([authorId])Add a database index on a foreign key or a column you filter on
@@unique([firstName, lastName])Compound unique constraint across two columns
@@id([postId, categoryId])Composite primary key, typical on join tables
@@map("users")Map the model to a differently named table
In v7 the CLI reads the datasource from prisma.config.ts; --url overrides it for one run on db pull, db push and migrate dev (7.2.0+).
npx prisma init --datasource-provider sqliteCreate the schema and config files for a new project (default provider is postgresql)
npx prisma generateWrite the typed client into the generator's output folder; run it after every schema change in v7
npx prisma migrate dev --name add_profileCreate a migration from schema changes and apply it to your dev database
npx prisma migrate dev --create-onlyWrite the migration SQL without applying it, so you can edit it first
npx prisma migrate deployApply pending migrations in CI or production; never resets or uses a shadow database
npx prisma migrate statusCompare the migrations folder with what the database has applied
npx prisma migrate reset --forceDrop the dev database, recreate it and reapply every migration, skipping the prompt
npx prisma db pushSync the schema straight to the database with no migration files
npx prisma db pullIntrospect an existing database and rewrite the schema to match
npx prisma db seedRun the seed command from prisma.config.ts; v7 no longer seeds for you
npx prisma studio --port 5555Open the local data browser (5555 is the default port)
Upgrade details are in https://www.prisma.io/docs/guides/upgrade-prisma-orm/v7. The docs pages read for this sheet don't mention the old package.json#prisma key at all, so move seed and schema settings into the config file.
import "dotenv/config";Load .env yourself at the top of the config; Prisma 7 doesn't (Bun does it on its own)
import { defineConfig, env } from "prisma/config";Helpers for a typed config and for reading required env vars
migrations: { path: "prisma/migrations", seed: "tsx prisma/seed.ts" }Where migrations live and which command db seed runs
datasource: { url: env("DATABASE_URL") }Connection string the CLI uses for migrate and db commands
generator client { provider = "prisma-client" output = "../src/generated/prisma" }v7 generator block; output is required
new PrismaClient({ adapter })Construct the client with a driver adapter such as @prisma/adapter-pg
Nested select is from https://www.prisma.io/docs/orm/prisma-client/queries/select-fields.
prisma.user.findUnique({ where: { email } })Fetch one row by an @id or @unique field
prisma.user.findFirst({ where: {...}, orderBy: { id: "desc" } })First row matching a non-unique filter
prisma.user.findMany({ where: { email: { endsWith: "prisma.io" } } })All rows matching a filter
prisma.user.findMany({ include: { posts: true } })Return each user with all of its related posts
select: { email: true, posts: { select: { title: true } } }Return only the listed fields, including inside a relation
prisma.user.createMany({ data: [...], skipDuplicates: true })Insert many rows and get back { count }, not the rows
prisma.user.upsert({ where, update, create })Update the row if the unique filter matches, otherwise create it
prisma.user.deleteMany({ where: { email: { contains: "test" } } })Delete every row that matches the filter in one query
await prisma.$transaction([q1, q2])Run independent queries in order inside one transaction; results come back as an array
await prisma.$transaction(async (tx) => { ... })Interactive transaction: use tx for every query; a throw rolls back
{ maxWait: 5000, timeout: 10000 }Raise the defaults of 2000 ms to start and 5000 ms to finish
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable }Override the database's default isolation level (not on MongoDB)
Worked example: Model users, profiles, posts and tags, then check the schema
Say you need users with an optional profile, posts by one author and categories shared across posts. Write the models with one relation of each kind:
model User {
id Int @id @default(autoincrement())
email String @unique
role Role @default(USER)
updatedAt DateTime @updatedAt
profile Profile?
posts Post[]
@@map("users")
}
model Profile {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int @unique
}
model Post {
id Int @id @default(autoincrement())
author User @relation(fields: [authorId], references: [id])
authorId Int
categories CategoriesOnPosts[]
@@index([authorId])
}
model CategoriesOnPosts {
post Post @relation(fields: [postId], references: [id])
postId Int
category Category @relation(fields: [categoryId], references: [id])
categoryId Int
@@id([postId, categoryId])
}
With Category and the Role enum added, running the validator gave this:
$ npx prisma validate --schema schema.prisma
Prisma schema loaded from schema.prisma
The schema at schema.prisma is valid
To see why @unique on Profile.userId matters, I removed it and validated again. The CLI refused with error P1012: a one-to-one relation must use unique fields on the defining side, so either add @unique or make it one-to-many. The @@index([authorId]) is there because the post list will filter by author. Once it validates, run npx prisma migrate dev --name init and then npx prisma generate, since in v7 the first command no longer triggers the second.
Common mistakes
Upgrading to Prisma 7 and wondering why the client types are stale after
migrate dev.Run
npx prisma generateyourself, or chain it in your npm script, because v7 removed the automatic generate step.Using
db pushagainst a database that other environments will migrate later.Use
migrate devso the change is recorded inprisma/migrations, thenmigrate deployeverywhere else.Running
migrate devormigrate resetin production because it worked locally.Both are development-only and can drop data; production only ever gets
migrate deploy.Calling
prisma.user.updateinside an interactive transaction instead oftx.user.update.Every query in the callback must go through
tx, otherwise it runs outside the transaction and won't roll back.Expecting
DATABASE_URLfrom.envto be picked up after the v7 upgrade.Add
import "dotenv/config"at the top ofprisma.config.tsand of your app entry point.Forgetting to index foreign keys used in filters on PostgreSQL.
Add
@@index([authorId])for columns you filter or join on; check the query plan if a list page feels slow.
Related cheat sheets
Sources
- https://www.prisma.io/docs/orm/reference/prisma-schema-reference
- https://www.prisma.io/docs/orm/prisma-schema/data-model/relations
- https://www.prisma.io/docs/orm/v7/prisma-schema/data-model/relations/many-to-many-relations
- https://www.prisma.io/docs/orm/reference/prisma-cli-reference
- https://www.prisma.io/docs/orm/reference/prisma-config-reference
- https://www.prisma.io/docs/guides/upgrade-prisma-orm/v7
- https://www.prisma.io/docs/orm/prisma-client/queries/crud
- https://www.prisma.io/docs/orm/prisma-client/queries/select-fields
- https://www.prisma.io/docs/orm/prisma-client/queries/transactions
- https://github.com/prisma/prisma/releases/tag/7.0.0
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.