Upgrade to Prisma ORM 4
Prisma ORM 4 introduces a number of breaking changes when you upgrade from an earlier Prisma ORM version. This guide explains how this upgrade might affect your application and gives instructions on how to handle any changes.
Breaking changes
This section gives an overview of breaking changes in Prisma ORM 4, grouped under general changes that affect both the Prisma Schema and Prisma Client, Schema changes and Client changes.
We recommend that you first address any Prisma schema validation errors, then pull your database to reflect new Prisma schema capabilities, and finally fix any type errors in Prisma Client and validate by running your test suite.
Upgrade your Prisma Schema
- Carefully skim the list of changes and check if you are impacted by a breaking change.
- Review the Prisma schema validation errors (via
npx prisma validate
, or via the Prisma VS Code extension).- If you don't have validation errors, continue with step 3.
- If you have validation errors:
- Try to map the validation error to a change from the list below to understand which change caused the invalid Prisma schema, and read the linked instructions for how to upgrade. It can only come from:
- Explicit unique constraints for 1:1 relations
- Removed support for usage of
references
on implicit many-to-many relations - Enforced uniqueness of referenced fields in the
references
argument in one-to-one and one-to-many relations for MySQL and MongoDB - Removal of undocumented support for the
type
alias - Removal of the
sqlite
protocol for SQLite URLs - Better grammar for string literals
- Try to map the validation error to a change from the list below to understand which change caused the invalid Prisma schema, and read the linked instructions for how to upgrade. It can only come from:
- Repeat until your Prisma schema is valid.
- Run
npx prisma db pull
to upgrade the Prisma schema to all new capabilities (e.g.extendedIndexes
). - Review changes of the Prisma schema and verify validity.
- Continue with Prisma Client steps.
Upgrade your use of Prisma Client
- Carefully skim the list of changes to understand if you are impacted by a breaking change.
- If yes, read the detailed upgrade instructions.
- If no, proceed with 2.
- Some API changes in Prisma Client are impacting runtime behavior, so please run your test suite.
Enjoy Prisma ORM 4!
General changes
This section includes changes that affect both the Prisma Schema and Prisma Client.
Node.js minimum version change
From Prisma ORM version 4.0.0, the minimum version of Node.js that we support is 14.17.x. If you use an earlier version of Node.js, you will need to update it.
See our system requirements for all minimum version requirements.
Schema changes
This section includes changes that affect the Prisma Schema.
Index configuration
In Prisma ORM 4, the extendedIndexes
Preview feature will now become generally available. This includes the following index configuration options:
- Length configuration of indexes, unique constraints and primary key constraints for MySQL (in Preview in versions 3.5.0 and later)
- Sort order configuration of indexes, unique constraints and primary key constraints (in Preview in versions 3.5.0 and later)
- New index types for PostgreSQL: Hash (in Preview in versions 3.6.0 and later) and GIN, GiST, SP-GiST and BRIN (in Preview in versions 3.14.0 and later)
- Index clustering for SQL Server (in Preview in versions 3.13.0 and later)
See our documentation on Index configuration for more details of these features.
Upgrade path
These can all be breaking changes if you were previously configuring these properties at the database level. In this case, you will need to:
- Upgrade to the new Prisma ORM 4 packages following these instructions
- Run
npx prisma db pull
afterwards to retrieve any existing configuration of indexes and constraints. This needs to be done before running anynpx prisma db push
ornpx prisma migrate dev
command, or you may lose any configuration that was defined in the database but not previously represented in the Prisma schema.
For more details, see the Upgrading from previous versions section of our index configuration documentation.
Scalar list defaults
For database connectors that support scalar lists (PostgreSQL, CockroachDB and MongoDB), Prisma ORM 4 introduces the ability to set a default value in your Prisma schema with the @default
attribute:
- Relational databases
- MongoDB
model User {
id Int @id @default(autoincrement())
posts Post[]
favoriteColors String[] @default(["red", "yellow", "purple"])
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
posts Post[]
favoriteColors String[] @default(["red", "yellow", "purple"])
}
Upgrade path
This is a breaking change if you previously had defaults defined for scalar lists at the database level. In this case, you will need to:
- Upgrade to the new Prisma ORM 4 packages following these instructions
- Run
npx prisma db pull
afterwards to retrieve any existing configuration of indexes and constraints. This needs to be done before running anynpx prisma db push
ornpx prisma migrate dev
command, or you will lose any defaults that are defined in the database but not previously represented in the Prisma schema.
Explicit @unique
constraints on one-to-one relations
When using one-to-one relations in Prisma ORM 4, you will need to explicitly add the @unique
attribute to the relation scalar field. For example, for this one-to-one relation between a User
and a Profile
model, you will need to add the @unique
attribute to the profileId
field:
- Relational databases
- MongoDB
model User {
id Int @id @default(autoincrement())
profile Profile? @relation(fields: [profileId], references: [id])
profileId Int? @unique // <-- include this explicitly
}
model Profile {
id Int @id @default(autoincrement())
user User?
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
profile Profile? @relation(fields: [profileId], references: [id])
profileId String? @unique @db.ObjectId // <-- include this explicitly
}
model Profile {
id String @id @default(auto()) @map("_id") @db.ObjectId
user User?
}
Upgrade path
After you upgrade to Prisma ORM 4, any one-to-one relations without a @unique
attribute on the relation scalar will trigger a validation error. To upgrade, you will need to:
-
Upgrade to the new Prisma ORM 4 packages following these instructions
-
Manually fix the validation errors in your Prisma schema by adding the explicit
@unique
or@id
attribute to your data model. -
Push the changes to your database using
prisma db push
for MongoDB orprisma migrate dev
for MySQL.
Enforced use of @unique
or @id
attribute for one-to-one and one-to-many relations (MySQL and MongoDB)
When you use one-to-one and one-to-many relations in Prisma ORM 4, you will need to use a @unique
attribute on the relation field to guarantee that the singular side(s) of the relation has only one record. This is now enforced for MySQL and MongoDB, bringing them into line with other connectors. Missing @unique
attributes will now trigger a validation error.
In the following example of a one-to-many relation between a User
and Post
model, the @unique
attribute must be added to the email
field:
- Relational databases
- MongoDB
model User {
id Int @id @default(autoincrement())
email String @unique // <-- we enforce this attribute
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
authorEmail String
author User @relation(fields: [authorEmail], references: [email])
}
model User {
id Int @id @default(auto()) @map("_id") @db.ObjectId
email String @unique // <-- we enforce this attribute
posts Post[]
}
model Post {
id Int @id @default(auto()) @map("_id") @db.ObjectId
authorEmail String
author User @relation(fields: [authorEmail], references: [email])
}
In the following example of a one-to-one relation between a User
and Profile
model, the @unique
attribute must be added to the email
field:
- Relational databases
- MongoDB
model User {
id Int @id @default(autoincrement())
email String @unique // <- we enforce this unique attribute
profile Profile @relation(fields: [profileId], references: [id])
profileId Int
}
model Profile {
id Int @id @default(autoincrement())
userEmail String? @unique
user User?
}
model User {
id Int @id @default(auto()) @map("_id") @db.ObjectId
email String @unique // <- we enforce this unique attribute
profile Profile @relation(fields: [profileId], references: [id])
profileId Int @db.ObjectId
}
model Profile {
id Int @id @default(auto()) @map("_id") @db.ObjectId
userEmail String? @unique
user User? @relation(fields: [userEmail], references: [email])
}
Upgrade path
After you upgrade to Prisma ORM 4, any one-to-one or one-to-many relations without a @unique
or @id
attribute on the relation field will trigger a validation error. To upgrade, you will need to:
- Upgrade to the new Prisma ORM 4 packages following these instructions
- Manually fix the validation errors in your Prisma schema. Alternatively, if you have an up-to-date live database, running
npx prisma db pull
will add the@unique
attributes automatically.
Disallow references
syntax for implicit many-to-many relations
When using implicit many-to-many relations in Prisma ORM 4, you will no longer be able to use the references
argument, which was previously optional. For example, the following relation would now trigger a validation error:
model Post {
id Int @id @default(autoincrement())
categories Category[] @relation("my-relation", references: [id]) // <-- validation error
}
model Category {
id Int @id @default(autoincrement())
posts Post[] @relation("my-relation", references: [id]) // <-- validation error
}
Instead, you can write:
model Post {
id Int @id @default(autoincrement())
categories Category[] @relation("my-relation")
}
model Category {
id Int @id @default(autoincrement())
posts Post[] @relation("my-relation")
}
This is because the only valid value for references
was id
, so removing this argument makes it clearer what can and cannot be changed.