Tags: prisma, orm, schema, migrations, relations, raw-queries, eager-loading, database Last updated: 2026-06-26

Prisma ORM Cheatsheet

Quick Reference

ConceptExample
Define modelmodel User { id Int @id @default(autoincrement()) }
Migrationnpx prisma migrate dev --name init
Generate clientnpx prisma generate
Find manyprisma.user.findMany({ where: {} })
Createprisma.user.create({ data: {} })
Updateprisma.user.update({ where: { id }, data: {} })
Include relationinclude: { posts: true }
Raw queryprisma.$queryRaw`SELECT ...`

Schema Syntax

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  name      String?
  posts     Post[]
  profile   Profile?
  createdAt DateTime @default(now())
  @@map("users")
}

model Post {
  id        Int     @id @default(autoincrement())
  title     String
  author    User    @relation(fields: [authorId], references: [id])
  authorId  Int
  tags      Tag[]
  @@map("posts")
}

model Tag {
  id    Int    @id @default(autoincrement())
  name  String @unique
  posts Post[]
}

Relations

// One-to-One:   profile Profile?  ↔  user User @unique
// One-to-Many:  posts Post[]       ↔  author User + authorId
// M-M implicit: tags Tag[]         ↔  posts Post[]
// M-M explicit: PostCategory join table with @@id

CRUD Operations

// Create
const user = await prisma.user.create({
  data: { email: "[email protected]", name: "Max" },
});

// Find
const users = await prisma.user.findMany({
  where: { role: "ADMIN" }, take: 10,
});
const user = await prisma.user.findUnique({ where: { id: 1 } });

// Upsert
const user = await prisma.user.upsert({
  where: { email: "[email protected]" },
  update: { name: "Maximilian" },
  create: { email: "[email protected]", name: "Max" },
});

// Delete
await prisma.user.delete({ where: { id: 1 } });

Eager Loading

const user = await prisma.user.findUnique({
  where: { id: 1 },
  include: { posts: { include: { tags: true } } },
  select: { id: true, email: true },
});

Migrations & Tools

npx prisma migrate dev --name add_field
npx prisma migrate deploy        # Production
npx prisma studio                # Data browser GUI
npx prisma generate              # Regenerate client
npx prisma format                # Format schema

Raw Queries & Transactions

const users = await prisma.$queryRaw`SELECT * FROM "users"`;
const [user, post] = await prisma.$transaction([
  prisma.user.create({ data: { ... } }),
  prisma.post.create({ data: { ... } }),
]);

Tips