If a repository item property references other items and the property’s cascade attribute is set to delete, removal of the repository item triggers removal of the referenced items. Also, when you remove a reference to this item, the item is automatically removed.

You should exercise caution when using cascading deletion in one-to-many relationships. Specifically, never set cascade to delete in properties on the “many” side of the relationship where those properties refer to items on the “one” side of the relationship. The item on the “one” side of the relationship cannot be deleted safely, because multiple items may be referring to it.

For example, an item descriptor company has an employee property that references many repository items defined by an employee item descriptor. The employee item descriptor itself defines a company property. In this one-to-many relationship, the employee property in the company item descriptor can set cascade to delete. However, the company property in the employee item descriptor must not set its own cascade attribute to delete; otherwise, the removal of one employee item would also entail removal of the company item that references all other employee items.

Removing null references

It sometimes happens that an item property references other items and one of those items is removed without explicitly removing the reference to it. This can occur when the database has no references constraint on the pertinent columns, so the referenced item can be removed without updating the referencing item’s property. It can also occur in a case where the referenced item might actually exist, but is currently filtered out by an RQL filter so it appears not to exist.

A repository item ignores references to missing items rather than returning an error if the referencing property sets its removeNullValues attribute to true. In this case, the missing item is returned as null to a scalar reference, and is omitted from the items that are returned for multi-valued references.

For example, a user profile might have a multi-valued property that is a list of favorite articles. Any given article might be deleted or become out of date. You can remove references to articles that are no longer available with the removeNullValues attribute like this:

<property name="favoriteArticles" data-type="list"
          component-item-type="articles">
  <attribute name="removeNullValues" value="true"/>
</property>
Order of deletion

Depending on how the database schema defines reference constraints, you might need to control whether a cascading deletion occurs before or after deletion of the referencing item. You can specify the desired behavior in an item descriptor by setting the cascadeDeleteOrder attribute:

<item-descriptor name="biographies" ...>
  <attribute name="cascadeDeleteOrder" value="last"/>
  <table name="...>
    <property name="publisher" item-type="publisher" cascade="delete"/>
    <property name="...>
</item-descriptor>

You can set cascadeDeleteOrder to one of these values:

Value

Description

afterAuxiliaryBeforePrimary

The default setting: cascading deletion is performed after deleting auxiliary multi-table rows, but before deleting the primary table row. This is the default behavior.

first

Cascading deletion is performed before any deletions in tables of this item.

last

Cascading deletion is performed after all deletions in tables of this item.