Skip to main content

DeletePolicy

@webda/models


Type Alias: DeletePolicy

DeletePolicy = "CASCADE" | "RESTRICT" | "SET_NULL" | "NONE"

Defined in: packages/models/src/relations.ts:545

What happens to the dependents of a model when it is deleted

Declared on the relation with the @OnDelete <POLICY> JSDoc tag (@CascadeDelete is a synonym of @OnDelete CASCADE), defaulted from the relation kind otherwise:

PolicyEffect when the target of the relation is deleted
CASCADEDelete every dependent, through the repository so its own events fire and it cascades in turn
RESTRICTRefuse the delete with a 409 Conflict while a dependent exists
SET_NULLRemove the attribute of each dependent (a single link) or the item (a link array)
NONEDo nothing
RelationDefault
ModelParent / BelongToCASCADE (SET_NULL is refused: a parent is mandatory)
ModelLink / RelateTo / ManyToOne / OneToOneNONE
ModelLinksSimpleArray / ModelLinksArray / ManyToManyNONE
Contains (declared on the owner)CASCADE: deleting the owner deletes the listed models

The RelationIntegrity service of @webda/core applies the policies, on the repository Delete / Deleted events: Repository.deleteMany emits none, so it never cascades.

class Comment extends UuidModel {
post: BelongTo<Post>; // CASCADE
/** @OnDelete RESTRICT */
author: BelongTo<User>; // a user with comments cannot be deleted
/** @OnDelete SET_NULL */
reviewer: RelateTo<User>;
}