rembrembdocs

Referential actions let you define the update and delete behavior of related models on the database level

Referential actions determine what happens to a record when your application deletes or updates a related record. They are defined in the @relation attribute and map to foreign key constraints in the database.

In the following example, onDelete: Cascade means that deleting a User record will also delete all related Post records.

schema.prisma

model Post {
  id       Int    @id @default(autoincrement())
  title    String
  author   User   @relation(fields: [authorId], references: [id], onDelete: Cascade)
  authorId Int
}

model User {
  id    Int    @id @default(autoincrement())
  posts Post[]
}

If you do not specify a referential action, Prisma ORM uses a default.

Questions answered in this page

Prisma ORM supports five referential actions:

Referential action defaults

If you do not specify a referential action, Prisma ORM uses the following defaults:

ClauseOptional relationsMandatory relations
onDeleteSetNullRestrict
onUpdateCascadeCascade

The following caveats apply:

The following table shows which referential action each database supports.

DatabaseCascadeRestrictNoActionSetNullSetDefault
PostgreSQL✔️✔️✔️✔️⌘✔️
MySQL/MariaDB✔️✔️✔️✔️❌ (✔️†)
SQLite✔️✔️✔️✔️✔️
SQL Server✔️❌‡✔️✔️✔️
CockroachDB✔️✔️✔️✔️✔️
MongoDB✔️✔️✔️✔️

Special cases for referential actions

Referential actions are part of the ANSI SQL standard. However, there are special cases where some relational databases diverge from the standard.

MySQL/MariaDB

MySQL/MariaDB, and the underlying InnoDB storage engine, does not support SetDefault. The exact behavior depends on the database version:

For this reason, when you set mysql as the database provider, Prisma ORM warns users to replace SetDefault referential actions in the Prisma schema with another action.

PostgreSQL

PostgreSQL is the only database supported by Prisma ORM that allows you to define a SetNull referential action that refers to a non-nullable field. However, this raises a foreign key constraint error when the action is triggered at runtime.

For this reason, when you set postgres as the database provider in the (default) foreignKeys relation mode, Prisma ORM warns users to mark as optional any fields that are included in a @relation attribute with a SetNull referential action. For all other database providers, Prisma ORM rejects the schema with a validation error.

SQL Server

Restrict is not available for SQL Server databases, but you can use NoAction instead.

Cascade

Example usage

schema.prisma

model Post {
  id       Int    @id @default(autoincrement())
  title    String
  author   User   @relation(fields: [authorId], references: [id], onDelete: Cascade, onUpdate: Cascade) 
  authorId Int
}

model User {
  id    Int    @id @default(autoincrement())
  posts Post[]
}

Result: If a User record is deleted, their posts are deleted too. If the user's id is updated, the corresponding authorId is also updated.

Restrict

Example usage

schema.prisma

model Post {
  id       Int    @id @default(autoincrement())
  title    String
  author   User   @relation(fields: [authorId], references: [id], onDelete: Restrict, onUpdate: Restrict) 
  authorId Int
}

model User {
  id    Int    @id @default(autoincrement())
  posts Post[]
}

Result: Users with posts cannot be deleted. The User's id cannot be changed.

NoAction

The NoAction action is similar to Restrict, the difference between the two is dependent on the database being used:

Example usage

schema.prisma

model Post {
  id       Int    @id @default(autoincrement())
  title    String
  author   User   @relation(fields: [authorId], references: [id], onDelete: NoAction, onUpdate: NoAction) 
  authorId Int
}

model User {
  id    Int    @id @default(autoincrement())
  posts Post[]
}

Result: Users with posts cannot be deleted. The User's id cannot be changed.

SetNull

SetNull will only work on optional relations. On required relations, a runtime error will be thrown since the scalar fields cannot be null.

schema.prisma

model Post {
  id       Int    @id @default(autoincrement())
  title    String
  author   User?  @relation(fields: [authorId], references: [id], onDelete: SetNull, onUpdate: SetNull) 
  authorId Int?
}

model User {
  id    Int    @id @default(autoincrement())
  posts Post[]
}

Result: When deleting or updating a User, the authorId is set to NULL for all their posts.

SetDefault

These require setting a default for the relation scalar field with @default. If no defaults are provided for any of the scalar fields, a runtime error will be thrown.

schema.prisma

model Post {
  id             Int     @id @default(autoincrement())
  title          String
  authorUsername String? @default("anonymous") 
  author         User?   @relation(fields: [authorUsername], references: [username], onDelete: SetDefault, onUpdate: SetDefault) 
}

model User {
  username String @id
  posts    Post[]
}

Result: When deleting or updating a User, their posts' authorUsername is set to the default value ('anonymous').

SQL Server doesn't allow cascading referential actions if the relation chain causes a cycle or multiple cascade paths. The server will return an error when executing the SQL.

MongoDB requires NoAction for self-referential relations or cycles between three models to prevent infinite loops. MongoDB uses relationMode = "prisma" by default, meaning Prisma ORM manages referential integrity.

Prisma ORM validates your data model before generating SQL, highlighting problematic relations to help you fix these issues early.

Self-relation (SQL Server and MongoDB)

The following model describes a self-relation where an Employee can have a manager and managees, referencing entries of the same model.

model Employee {
  id        Int        @id @default(autoincrement())
  manager   Employee?  @relation(name: "management", fields: [managerId], references: [id])
  managees  Employee[] @relation(name: "management")
  managerId Int?
}

This will result in the following error:

Error parsing attribute "@relation": A self-relation must have `onDelete` and `onUpdate` referential actions set to `NoAction` in one of the @relation attributes. (Implicit default `onDelete`: `SetNull`, and `onUpdate`: `Cascade`)

By not defining any actions, Prisma ORM will use the following default values depending if the underlying scalar fields are set to be optional or required.

ClauseAll of the scalar fields are optionalAt least one scalar field is required
onDeleteSetNullNoAction
onUpdateCascadeCascade

Since the default referential action for onUpdate in the above relation would be Cascade and for onDelete it would be SetNull, it creates a cycle and the solution is to explicitly set the onUpdate and onDelete values to NoAction.

model Employee {
  id        Int        @id @default(autoincrement())
  manager   Employee   @relation(name: "management", fields: [managerId], references: [id]) 
  manager   Employee   @relation(name: "management", fields: [managerId], references: [id], onDelete: NoAction, onUpdate: NoAction) 
  managees  Employee[] @relation(name: "management")
  managerId Int
}

Cyclic relation between three tables (SQL Server and MongoDB)

The following models describe a cyclic relation between a Chicken, an Egg and a Fox, where each model references the other.

model Chicken {
  id        Int   @id @default(autoincrement())
  egg       Egg   @relation(fields: [eggId], references: [id])
  eggId     Int
  predators Fox[]
}

model Egg {
  id         Int       @id @default(autoincrement())
  predator   Fox       @relation(fields: [predatorId], references: [id])
  predatorId Int
  parents    Chicken[]
}

model Fox {
  id        Int     @id @default(autoincrement())
  meal      Chicken @relation(fields: [mealId], references: [id])
  mealId    Int
  foodStore Egg[]
}

This will result in validation errors indicating a cycle exists:

Error parsing attribute "@relation": Reference causes a cycle. One of the @relation attributes in this cycle must have `onDelete` and `onUpdate` referential actions set to `NoAction`. Cycle path: Chicken.egg → Egg.predator → Fox.meal. (Implicit default `onUpdate`: `Cascade`)

Since the default onUpdate action is Cascade, it creates a cycle. Set onUpdate: NoAction on any one of the relations to break the cycle:

model Chicken {
  id        Int   @id @default(autoincrement())
  egg       Egg   @relation(fields: [eggId], references: [id]) 
  egg       Egg   @relation(fields: [eggId], references: [id], onUpdate: NoAction) 
  eggId     Int
  predators Fox[]
}

Multiple cascade paths between two models (SQL Server only)

The data model describes two different paths between same models, with both relations triggering cascading referential actions.

model User {
  id       Int       @id @default(autoincrement())
  comments Comment[]
  posts    Post[]
}

model Post {
  id       Int       @id @default(autoincrement())
  authorId Int
  author   User      @relation(fields: [authorId], references: [id])
  comments Comment[]
}

model Comment {
  id          Int  @id @default(autoincrement())
  writtenById Int
  postId      Int
  writtenBy   User @relation(fields: [writtenById], references: [id])
  post        Post @relation(fields: [postId], references: [id])
}

There are two paths from Comment to User, and the default onUpdate: Cascade creates multiple cascade paths:

Error parsing attribute "@relation": When any of the records in model `User` is updated or deleted, the referential actions on the relations cascade to model `Comment` through multiple paths. Please break one of these paths by setting the `onUpdate` and `onDelete` to `NoAction`. (Implicit default `onUpdate`: `Cascade`)

Set onUpdate: NoAction on any one of the relations to break the multiple cascade paths:

model Comment {
  id          Int  @id @default(autoincrement())
  writtenById Int
  postId      Int
  writtenBy   User @relation(fields: [writtenById], references: [id]) 
  writtenBy   User @relation(fields: [writtenById], references: [id], onUpdate: NoAction) 
  post        Post @relation(fields: [postId], references: [id])
}