Skip to main content

OnDelete (Property Keyword)

Specifies the action to take in the current table when a related object is deleted. This keyword applies only to a relationship property that specifies Cardinality as Parent or One. Its use is invalid in all other contexts.

Usage

To specify the action to take in the current table when a related object is deleted, use the following syntax:

Relationship relname As classname [ Cardinality = cardinality, Inverse = inverse, OnDelete = ondelete ];

Where ondelete is one of the following values. In this discussion, related record is a record or object belonging to the other side of the relationship, and referencing record is the record or object in this side of the relationship.

  • cascade — When a related record is deleted, the referencing record in this table is also deleted.

  • noaction — When an attempt is made to delete a related record, the attempt fails.

  • setdefault — When a related record is deleted, the referencing record in this table is set to its default value.

  • setnull — When a related record is deleted, the referencing record in this table is set to null.

Details

This keyword defines the referential action that occurs when a record is deleted on the other side of a relationship.

Default

If you omit this keyword, then:

  • For a relationship with Cardinality as Parent, OnDelete is cascade. That is, when you delete the parent record, by default, the associated child records are deleted.

  • For a relationship with Cardinality as One, OnDelete is noaction. That is, when you attempt to delete the one record, by default, the attempt fails if the other table has any records that point to it.

Example

Class MyApp.Employee Extends %Persistent {  
...
Relationship Employer As MyApp.Company [ Cardinality = one, Inverse = Employees, OnDelete = cascade ];
}

This example shows a one-to-many relationship between a company and its employees. As shown here, the Cardinality signifies that company is the “one” side of the relationship, while OnDelete indicates the effect of deleting the company on its employees. As the value of OnDelete is cascade, when a company is deleted, its effect cascades and the employees are also deleted.

If the value of OnDelete is noaction (the default for one-to-many relationships), deleting a company is not permitted if any employee has the company as its employer.

If the value of OnDelete is setnull or setdefault, deleting a company sets the employer of any of its employees to null.

See Also

FeedbackOpens in a new tab