apps/docs/content/docs/guides/upgrade-prisma-orm/v4.mdx
This guide helps you upgrade your project to Prisma ORM 4. Version 4 includes several important changes that may affect your application, so please review this guide carefully before proceeding.
Schema Changes
@unique constraints for one-to-one relations@unique or @id for one-to-one and one-to-many relations in MySQL/MongoDBClient changes
queryRaw return type changesTo upgrade to Prisma ORM 4 from an earlier version, you need to update both the prisma and @prisma/client packages:
npm install @prisma/client@4
npm install -D prisma@4
npx prisma generate
:::danger
Before you upgrade, check each breaking change below to see how the upgrade might affect your application.
:::
Run the following command to check for schema validation errors:
npx prisma validate
Address any validation errors, paying special attention to:
Explicit @unique constraints for one-to-one relations
@unique to relation scalar fields in one-to-one relations// Before
model User {
id Int @id @default(autoincrement())
profile Profile? @relation(fields: [profileId], references: [id])
profileId Int?
}
// After
model User {
id Int @id @default(autoincrement())
profile Profile? @relation(fields: [profileId], references: [id])
profileId Int? @unique // Added @unique
}
Enforced @unique or @id for relations in MySQL/MongoDB
@unique or @id// Before (MySQL/MongoDB)
model User {
id Int @id @default(autoincrement())
email String // Missing @unique
posts Post[]
}
// After (MySQL/MongoDB)
model User {
id Int @id @default(autoincrement())
email String @unique // Added @unique
posts Post[]
}
Update SQLite connection strings
sqlite: protocol prefix if presentsqlite:./dev.dbfile:./dev.dbString literals
a string with \"quotes\"a string with \"quotes\" or a string with "quotes"After fixing validation errors, pull the latest schema changes:
npx prisma db pull
This will update your schema with new capabilities like improved index configuration.
queryRaw Return Type ChangesIn Prisma 4, the return types of queryRaw and queryRawUnsafe have changed:
| Data Type | Before 4.0.0 | From 4.0.0 |
|---|---|---|
DateTime | String | Date |
BigInt | String | BigInt |
Bytes | String | Buffer |
Decimal | String | Decimal |
Update your code to handle these type changes, especially if you're using TypeScript or performing type checks.
Prisma 4 changes how JSON null values are handled. If you're working with JSON fields:
// Before
const result = await prisma.model.findMany({
where: {
jsonField: { equals: null }
}
});
// After
import { Prisma } from '@prisma/client';
const result = await prisma.model.findMany({
where: {
jsonField: { equals: Prisma.JsonNull }
}
});
For MongoDB users, Prisma 4 changes how default fields on composite types work. If you're using MongoDB with composite types, test your queries to ensure they return the expected results.
After updating your schema and code, thoroughly test your application:
The extendedIndexes feature is now generally available, with support for:
Example usage:
model User {
id Int @id
email String @unique(sort: Desc)
name String
@@index([name(sort: Asc, length: 10)])
@@index([email], type: Hash) // PostgreSQL specific
}
You can now set default values for scalar lists in your schema:
model User {
id Int @id @default(autoincrement())
favoriteColors String[] @default(["red", "blue", "green"])
luckyNumbers Int[] @default([7, 42])
}
Prisma 4 improves type safety with:
@unique constraintsSchema Validation Errors
@unique constraints@unique to required relation fieldsType Errors in queryRaw
JSON Null Handling
Prisma.JsonNull, Prisma.DbNull, or Prisma.AnyNull explicitlyIf you encounter issues during the upgrade: