References and on_delete
Any field typed X.id is a reference to a record of X. The database
gets a foreign key and an index for it. Its block must say what happens
when the record it refers to is deleted; there is no default.
entity User { id: Uuid}entity Project { id: Uuid}entity Todo { id: Uuid owner_id: User.id { on_delete: OnDelete.restricted // a user with todos cannot be deleted } project_id: Project.id? { on_delete: OnDelete.cleared // the todo stays; it leaves the project }}
Here a user who still has todos can't be deleted, and a todo whose project is deleted stays, outside any project.
on_delete: |
When the referenced record is deleted | Allowed on |
|---|---|---|
OnDelete.restricted |
The delete fails with a conflict while any record refers to it | any reference |
OnDelete.cascade |
Records that refer to it are deleted in the same transaction | any reference |
OnDelete.cleared |
The reference is set to none |
an optional reference (Project.id?) |
A cycle of required references is an error, because neither record could be created first.
Markdown for agents: /docs/references-and-on-delete.md.Built from Jadpo main at a707efb.