Understanding Referential Actions

1. Using Cascade Delete

AspectDetail
EffectDeletes child rows when parent deleted
SyntaxonDelete: Cascade
Use caseComments under a post, audit trail per resource

2. Using Set Null

RequirementDetail
FK must be optionale.g., authorId Int?
BehaviorFK nulled when parent deleted/updated

3. Using Set Default

RequirementDetail
FK default setauthorId Int @default(0)
BehaviorFK reset to default when parent deleted

4. Using Restrict

BehaviorDetail
ImmediatePrevents delete if dependents exist
ThrowsP2003 foreign key error

5. Using No Action

AspectDetail
DefaultUsed when no action specified (depends on DB)
PGDeferred check at end of transaction
MongoDBOnly NoAction or Cascade with prisma mode

6. Configuring Update Actions

OptionEffect
onUpdate: CascadePropagate PK change to FKs
onUpdate: RestrictBlock PK update if FK exists
When to useRarely; immutable PKs preferred

7. Combining Delete and Update Actions

author User @relation(fields:[authorId], references:[id],
                       onDelete: Cascade, onUpdate: Restrict)
PairEffect
Cascade + RestrictDelete children but block PK rename
SetNull + CascadeUpdate propagates, deletes detach

8. Understanding Database-Specific Behavior

EngineCaveat
SQL ServerMultiple cascade paths to same table forbidden
PGSupports all actions natively
MongoDBEmulated in Prisma client (relationMode = "prisma")

9. Handling Circular References

Warning: Two-way cascading deletes between models can create infinite cycles in some engines. Choose one direction as Cascade and the other as Restrict or NoAction.
RiskMitigation
CycleUse SetNull on one side
Detectionprisma migrate dev emits error

10. Validating Referential Integrity

ModeBehavior
relationMode = "foreignKeys"Native DB constraints (default)
relationMode = "prisma"Prisma emulates (for PlanetScale/serverless)
Trade-offApp-level mode requires explicit indexes