Skip to main content
Version: 2.0.0

Referential actions

In Serverpod, the behavior of update and delete for relations can be precisely defined using the onUpdate and onDelete properties. These properties map directly to the corresponding referential actions in PostgreSQL.

Available Referential Actions

ActionDescription
NoActionIf any constraint violation occurs, no action will be taken, and an error will be raised.
RestrictIf any referencing rows still exist when the constraint is checked, an error is raised.
SetDefaultThe field will revert to its default value. Note: This action necessitates that a default value is configured for the field.
CascadeAny action taken on the parent (update/delete) will be mirrored in the child.
SetNullThe field value is set to null. This action is permissible only if the field has been marked as optional.

Syntax

Use the following syntax to apply referential actions

relation(onUpdate=<ACTION>, onDelete=<ACTION>)

For instance, the default behavior in Serverpod can be expressed as.

relation(onUpdate=NoAction, onDelete=Cascade)
info

The sequence of onUpdate and onDelete is interchangeable.

Full example

class: Example
table: example
fields:
parentId: int?, relation(parent=example, onUpdate=SetNull, onDelete=NoAction)

In the given example, if the example parent is updated, the parentId will be set to null. If the parent is deleted, no action will be taken for parentId.