apps/docs/content/docs/guides/upgrade-prisma-orm/v3.mdx
This guide helps you upgrade your project to Prisma ORM v3. Version 3 includes several important changes that may affect your application, so please review this guide carefully before proceeding.
Prisma ORM 3 introduces native support for referential actions, which changes how cascading deletes are handled:
model User {
id Int @id @default(autoincrement())
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
author User @relation(fields: [authorId], references: [id])
authorId Int
}
model User {
id Int @id @default(autoincrement())
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
author User @relation(
fields: [authorId],
references: [id],
onDelete: Cascade, // Explicit action required
onUpdate: Cascade // Optional: define update behavior
)
authorId Int
}
Action Required: Review all your relation fields and add appropriate onDelete and onUpdate actions.
Prisma ORM 3 changes how constraints and indexes are named:
map (database-level name) and name (Prisma Client API name)If you need to maintain existing constraint names (for compatibility with other tools or conventions):
prisma db pull to update your schema with existing constraint names@map attributes for non-default constraint namesTo use Prisma ORM's new default naming convention:
prisma migrate dev to generate a migration updating constraint namesExample of constraint naming in schema:
model User {
id Int @id(map: "Custom_Primary_Key") @default(autoincrement())
email String @unique(map: "Custom_Unique_Constraint")
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
author User @relation(fields: [authorId], references: [id], map: "Custom_Foreign_Key")
authorId Int
}
$queryRaw Method ChangesThe $queryRaw method now only supports template literals for security:
// String syntax (no longer supported in v3)
const result = await prisma.$queryRaw('SELECT * FROM User WHERE id = 1');
// Template literal syntax (recommended)
const userId = 1;
const result = await prisma.$queryRaw`SELECT * FROM User WHERE id = ${userId}`;
// Or use $queryRawUnsafe (use with caution)
const unsafeResult = await prisma.$queryRawUnsafe(
'SELECT * FROM User WHERE id = $1',
userId
);
Action Required: Update all $queryRaw calls in your codebase to use template literals or switch to $queryRawUnsafe with proper parameterization.
Prisma ORM 3 introduces more precise handling of null values in JSON fields:
{ equals: null } now checks for database NULL valuesPrisma.JsonNull: Represents a JSON null valuePrisma.DbNull: Represents a database NULL valuePrisma.AnyNull: Matches either JSON null or database NULL// Ambiguous behavior in v2
const result = await prisma.log.findMany({
where: {
meta: {
equals: null // Could mean either JSON null or database NULL
}
}
});
import { Prisma } from '@prisma/client';
// Check for JSON null
const jsonNullResults = await prisma.log.findMany({
where: {
meta: {
equals: Prisma.JsonNull
}
}
});
// Check for database NULL
const dbNullResults = await prisma.log.findMany({
where: {
meta: {
equals: Prisma.DbNull
}
}
});
// Check for either
const anyNullResults = await prisma.log.findMany({
where: {
meta: {
equals: Prisma.AnyNull
}
}
});
// Creating with specific null types
await prisma.log.create({
data: {
meta: Prisma.JsonNull // or Prisma.DbNull
}
});
Action Required: Update all JSON field operations that involve null values to use the appropriate null type.
To upgrade to Prisma ORM 3 from an earlier version, you need to update both the prisma and @prisma/client packages:
npm install @prisma/client@3
npm install -D prisma@3
npx prisma generate
:::note
For yarn, use yarn up prisma@3 @prisma/client@3. For pnpm, use pnpm upgrade prisma@3 @prisma/client@3.
:::
:::danger
Before you upgrade, check each breaking change below to see how the upgrade might affect your application.
:::
onDelete and onUpdate actions for all relations.$queryRaw calls to use template literals or switch to $queryRawUnsafe.Missing Referential Actions
onDelete and onUpdate actions to your relationsJSON Null Handling
Prisma.JsonNull, Prisma.DbNull, or Prisma.AnyNullRaw Query Errors
$queryRaw$queryRawUnsafe