1class OnDelete:
2 """Sentinel marking an on_delete action.
3
4 Each valid action is a single `OnDelete` instance exported from
5 ``plain.postgres`` (CASCADE, SET_NULL, RESTRICT). Cascading is enforced
6 entirely by Postgres via the corresponding ``ON DELETE`` clause — there
7 is no application-level traversal.
8 """
9
10 __slots__ = ("confdeltype", "name", "sql_clause")
11
12 def __init__(self, name: str, sql_clause: str, confdeltype: str) -> None:
13 self.name = name
14 self.sql_clause = sql_clause
15 self.confdeltype = confdeltype
16
17 def __repr__(self) -> str:
18 return f"<plain.postgres.{self.name}>"
19
20
21#: Child rows are deleted by Postgres when the parent is deleted.
22CASCADE = OnDelete("CASCADE", " ON DELETE CASCADE", "c")
23
24#: Child FK columns are set to NULL when the parent is deleted.
25#: Requires ``allow_null=True`` on the field.
26SET_NULL = OnDelete("SET_NULL", " ON DELETE SET NULL", "n")
27
28#: Deleting the parent fails immediately with IntegrityError if children exist.
29RESTRICT = OnDelete("RESTRICT", " ON DELETE RESTRICT", "r")