Skip to main content
Version: 2.3.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>)

Default values

If no referential actions are specified, the default behavior will be applied.

If the relation is defined as an object relation, the default behavior is NoAction for both onUpdate and onDelete.

parent: Model?, relation(onUpdate=NoAction, onDelete=NoAction)

If the relation is defined as an id relation, the default behavior is NoAction for onUpdate and Cascade for onDelete.

parentId: int?, relation(parent=model_table, 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.