Skip to content

Commit 477c4b8

Browse files
patricebenderstewskrenejeglinskygithub-actions[bot]smahati
authored
docs: re-add paragraph about ON DELETE CASCADE (#2513)
this is an important distinction. Users open issues because they dont understand the feature scope, e.g. cap/issues/issues/20464 --------- Co-authored-by: Steffen Weinstock <79531202+stewsk@users.noreply.github.com> Co-authored-by: René Jeglinsky <rene.jeglinsky@sap.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Mahati Shankar <93712176+smahati@users.noreply.github.com>
1 parent 429e551 commit 477c4b8

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

guides/databases/cdl-to-ddl.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,40 @@ CREATE TABLE Books ( ...
611611
:::
612612

613613
> [!tip] Consider using <code>@assert.target</code> instead
614-
> Database constraints are meant to protect against data corruption due to programming errors. Prefer using the [`@assert.target`](../services/constraints#assert-target) for application-level input validation, which is more tuned for typical application scenarios, with error messages taylored for end users.
614+
> Database constraints are meant to protect against data corruption due to programming errors. Prefer using the [`@assert.target`](../services/constraints#assert-target) for application-level input validation, which is more tuned for typical application scenarios, with error messages tailored for end users.
615+
616+
#### `ON DELETE CASCADE`
617+
618+
Think of the example above: a book can still exist, even if its author is deleted. However, this differs for existential relationships like the one between a book and its pages: a page cannot exist without a corresponding book. In such cases, you want the pages to be automatically deleted when the book (parent) is deleted.
619+
620+
Typically such existential relationships are modeled in CDS using [compositions](../../cds/cdl#compositions).
621+
That is why for managed **backlink** associations (those used in a composition's on-condition via `$self = <comp>.<backlink>`), the foreign key constraints are generated with `ON DELETE CASCADE` by default, instead of `ON DELETE RESTRICT`:
622+
623+
::: code-group
624+
```cds [CDS Source]
625+
entity Books {
626+
key ID : Integer;
627+
pages: Composition of many Pages on pages.book = $self;
628+
}
629+
entity Pages {
630+
key number: Integer;
631+
key book: Association to Books; // → ON DELETE CASCADE
632+
}
633+
```
634+
:::
635+
636+
::: code-group
637+
```sql [=> Generated DDL]
638+
639+
ALTER TABLE Pages ADD CONSTRAINT c__Pages_book
640+
FOREIGN KEY(book_ID) REFERENCES Books(ID)
641+
ON UPDATE RESTRICT
642+
ON DELETE CASCADE
643+
VALIDATED
644+
ENFORCED
645+
INITIALLY DEFERRED;
646+
```
647+
:::
615648

616649
#### Skipping with `@assert.integrity:false`
617650

0 commit comments

Comments
 (0)