bcquality/community/knowledge/data-modeling/owning-table-must-delete-dependents-in-ondelete.md
Stefan Maron e00ef8ad94 knowledge(data-modeling): TableRelation delete/rename asymmetry and the xRec before-image contract
Three related concerns, each proven by executable tests rather than recall.
They were extracted from a real defect that shipped through a six-reviewer
panel undetected, which is the admission test passing on behavior rather than
on theory.

owning-table-must-delete-dependents-in-ondelete
  AL has no cascading delete. What makes it missable is an asymmetry: the
  platform DOES keep references correct on rename via TableRelation, so a
  developer who learns that and generalizes it to delete ships orphans. Also
  notes the permission trap (delete rights needed on the dependent table, not
  just the parent).

validate-table-relation-false-suppresses-rename-propagation
  The non-obvious half. The property name implies input validation only, but
  disabling it also switches off rename propagation. Verified against a parent
  renamed once while a child held three fields: a normal relation (follows),
  the same relation with validation disabled (does NOT follow), and a field
  with no relation at all (does not follow) — the third being the control that
  proves the test can detect a non-propagating field.

xrec-is-a-before-image-only-in-some-triggers
  Corrects both the naive belief that xRec is always the previous record and
  the folk rule that it 'only works from a page'. The behavior is per-trigger:
  a genuine before-image in OnRename and OnDelete regardless of driver, a
  mirror of Rec in OnInsert/OnModify when driven from code, and a real
  before-image in those two only when a page drove the write. That last
  asymmetry is why an OnModify comparison against xRec passes manual page
  testing and silently no-ops in a job queue.

Targets /community per CONTRIBUTING — general BC knowledge, not fork-specific.
Frontmatter validator clean; Test-ReviewFixtures passes (32 cases, 16 leaves).
2026-08-05 13:46:41 +02:00

36 lines
2.4 KiB
Markdown

---
bc-version: [all]
domain: data-modeling
keywords: [ondelete, cascade, table-relation, orphan-records, header-line, dependent-records, referential-integrity]
technologies: [al]
countries: [w1]
application-area: [all]
---
# A table that owns dependent records must delete them in `OnDelete`
## Description
`TableRelation` looks like referential integrity but only performs lookup and input validation. AL has **no cascading delete**: deleting a parent record leaves every dependent row untouched, and no error is raised.
What makes this specifically missable is an asymmetry. The platform *does* keep references correct on **rename** — renaming a record updates it in all other locations that declare a `TableRelation` to it, with no code. Delete has no equivalent. Same relation, same metadata, opposite behaviour. A developer who correctly learns that `TableRelation` "keeps references consistent" from the rename case, and generalises it to delete, ships orphans.
Orphaned rows are usually invisible, because a dependent table rarely has a page of its own. They inflate the table, break later reconciliation, and are re-encountered by duplicate checks when the parent key is reused.
This applies to internal, staging and `SystemMetadata` tables too. A table having no delete action in the UI today is not protection: a permission set that grants `D` on the table is evidence that deletion is anticipated.
See also `validate-table-relation-false-suppresses-rename-propagation.md` for the two preconditions on the rename half of this asymmetry.
## Best Practice
The owning table implements `OnDelete` and deletes its dependents there, filtered on the foreign key. Declare `Permissions = tabledata <dependent> = rd` on the owning table — granting delete rights only on the parent is a common miss that makes the trigger fail for a non-`SUPER` user. This mirrors the base application, where every header table deletes its own lines.
See sample: `owning-table-must-delete-dependents-in-ondelete.good.al`.
## Anti Pattern
A parent table with dependent rows and no `OnDelete` trigger, where the dependent's foreign-key field declares a `TableRelation` back to the parent. The relation reads as if it guarantees integrity; it does not.
Detection signal: a table declares `TableRelation` to table X, and table X has no `OnDelete` trigger. Whether a delete path currently exists in the UI is irrelevant to the finding.
See sample: `owning-table-must-delete-dependents-in-ondelete.bad.al`.