Add ON DELETE and ON UPDATE support to Django
|Reported by:||glassfordm||Owned by:||carljm|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||miracle2k, mathijs@…, matthubb, diegobz, road, arikon, marinho, me@…, findepi, gabor@…, dnordberg@…, ehs@…, jtiai, hanne.moa@…, aaron@…, groby@…, semenov, ales.zoulek@…, plandry@…, lukasz.korzybski@…, preston@…, eschler@…, paluho@…, gorsky.exe@…, ego@…, gonz, l.dziedzia@…, cgieringer, t.django@…, goliath.mailinglist@…, jdunck@…, mlong@…, erikrose, paulc@…, jakub@…, david@…, jbauer@…, luc.saffre@…||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||yes|
|Needs tests:||no||Patch needs improvement:||yes|
The attached patch adds two levels of ON DELETE and ON UPDATE
support to Django.
- Just applying the patch changes nothing: Django should continue
to behave exactly as before, emulating ON DELETE CASCADE behavior
just as it has all along.
1) Adding on_delete=RESTRICT or on_delete=SET_NULL to a ForeignKey
definition will cause Django to emulate the specified behavior
for that particular foreign key. Django will continue to emulate
CASCADE behavior for ForeignKeys which have no on_delete behavior
specified; it will also emulate CASCADE behavior for foreign keys
having on_delete=CASCADE defined, of course.
2) The new setting, settings.ON_DELETE_NONE_HANDLING = <option>,
specifies the behavior of foreign keys that have no on_delete
option specified. It defaults to CASCADE (because this is what
Django has done in the past), but can be set to RESTRICT (essentially
the default for SQL) or SET_NULL as well (although SET_NULL is not
recommended: it will cause errors for ForeignKeys that are not nullable).
3) Finally, the patch does not provide any Django emulation for
ON UPDATE behavior, so, while the on_update option can be added to
foreign key definitions, it does nothing (but see below).
- Defining settings.ON_DELETE_HANDLED_BY_DB = True changes Django's
ON DELETE behavior completely:
1) It disables Django emulation of ON DELETE behavior entirely.
2) It adds appropriate ON DELETE statements to the SQL generated
by Django (by "python manage.py sqlall", for instance).
- Defining settings.ON_UPDATE_HANDLED_BY_DB = True adds appropriate
ON UPDATE statements to the SQL generated by Django
(by "python manage.py sqlall", for instance).
Change History (80)
comment:1 Changed 7 years ago by edgarsj
- Component changed from Uncategorized to Database wrapper
- Keywords feature added
- milestone set to post-1.0
- Triage Stage changed from Unreviewed to Design decision needed
Changed 6 years ago by dokterbob
comment:22 Changed 6 years ago by findepi
- Cc findepi added
- Owner changed from nobody to findepi
- Status changed from new to assigned
comment:61 Changed 5 years ago by carljm
- Triage Stage changed from Design decision needed to Accepted