Skip to content

Menu

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 Bytes

The 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 @unique

Adds a unique constraint on one column

createdAt DateTime @default(now())

Fills the timestamp when the row is created

updatedAt DateTime @updatedAt

Prisma refreshes the timestamp on every update

nickname String @map("nick_name")

Keep a camelCase field name while the column is snake_case

From the docs, not run by us
bio String @db.Text

Pick a native column type instead of the default mapping

From the docs, not run by us

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 @unique

One-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

From the docs, not run by us
@@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 sqlite

Create the schema and config files for a new project (default provider is postgresql)

From the docs, not run by us
npx prisma generate

Write the typed client into the generator's output folder; run it after every schema change in v7

From the docs, not run by us
npx prisma migrate dev --name add_profile

Create a migration from schema changes and apply it to your dev database

Try it: migration-sql-generatorFrom the docs, not run by us
npx prisma migrate dev --create-only

Write the migration SQL without applying it, so you can edit it first

From the docs, not run by us
npx prisma migrate deploy

Apply pending migrations in CI or production; never resets or uses a shadow database

From the docs, not run by us
npx prisma migrate status

Compare the migrations folder with what the database has applied

From the docs, not run by us
npx prisma migrate reset --force

Drop the dev database, recreate it and reapply every migration, skipping the prompt

From the docs, not run by us
npx prisma db push

Sync the schema straight to the database with no migration files

From the docs, not run by us
npx prisma db pull

Introspect an existing database and rewrite the schema to match

From the docs, not run by us
npx prisma db seed

Run the seed command from prisma.config.ts; v7 no longer seeds for you

From the docs, not run by us
npx prisma studio --port 5555

Open the local data browser (5555 is the default port)

From the docs, not run by us

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)

From the docs, not run by us
import { defineConfig, env } from "prisma/config";

Helpers for a typed config and for reading required env vars

From the docs, not run by us
migrations: { path: "prisma/migrations", seed: "tsx prisma/seed.ts" }

Where migrations live and which command db seed runs

From the docs, not run by us
datasource: { url: env("DATABASE_URL") }

Connection string the CLI uses for migrate and db commands

From the docs, not run by us
generator client { provider = "prisma-client" output = "../src/generated/prisma" }

v7 generator block; output is required

From the docs, not run by us
new PrismaClient({ adapter })

Construct the client with a driver adapter such as @prisma/adapter-pg

From the docs, not run by us

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

From the docs, not run by us
prisma.user.findFirst({ where: {...}, orderBy: { id: "desc" } })

First row matching a non-unique filter

From the docs, not run by us
prisma.user.findMany({ where: { email: { endsWith: "prisma.io" } } })

All rows matching a filter

From the docs, not run by us
prisma.user.findMany({ include: { posts: true } })

Return each user with all of its related posts

From the docs, not run by us
select: { email: true, posts: { select: { title: true } } }

Return only the listed fields, including inside a relation

From the docs, not run by us
prisma.user.createMany({ data: [...], skipDuplicates: true })

Insert many rows and get back { count }, not the rows

From the docs, not run by us
prisma.user.upsert({ where, update, create })

Update the row if the unique filter matches, otherwise create it

From the docs, not run by us
prisma.user.deleteMany({ where: { email: { contains: "test" } } })

Delete every row that matches the filter in one query

From the docs, not run by us
await prisma.$transaction([q1, q2])

Run independent queries in order inside one transaction; results come back as an array

From the docs, not run by us
await prisma.$transaction(async (tx) => { ... })

Interactive transaction: use tx for every query; a throw rolls back

From the docs, not run by us
{ maxWait: 5000, timeout: 10000 }

Raise the defaults of 2000 ms to start and 5000 ms to finish

From the docs, not run by us
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable }

Override the database's default isolation level (not on MongoDB)

From the docs, not run by us

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 generate yourself, or chain it in your npm script, because v7 removed the automatic generate step.

  • Using db push against a database that other environments will migrate later.

    Use migrate dev so the change is recorded in prisma/migrations, then migrate deploy everywhere else.

  • Running migrate dev or migrate reset in production because it worked locally.

    Both are development-only and can drop data; production only ever gets migrate deploy.

  • Calling prisma.user.update inside an interactive transaction instead of tx.user.update.

    Every query in the callback must go through tx, otherwise it runs outside the transaction and won't roll back.

  • Expecting DATABASE_URL from .env to be picked up after the v7 upgrade.

    Add import "dotenv/config" at the top of prisma.config.ts and 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.