Add ON DELETE and ON UPDATE support to Django
|Reported by:||glassfordm||Owned by:||Carl Meyer|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||miracle2k, mathijs@…, matthubb, diegobz, road, Sergey Belov, Marinho Brandão, me@…, findepi, gabor@…, dnordberg@…, ehs@…, Jani Tiainen, hanne.moa@…, aaron@…, groby@…, Ilya Semenov, ales.zoulek@…, plandry@…, lukasz.korzybski@…, preston@…, eschler@…, paluho@…, gorsky.exe@…, ego@…, Gonzalo Saavedra, 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 8 years ago by
|Component:||Uncategorized → Database wrapper|
|Triage Stage:||Unreviewed → Design decision needed|
comment:22 Changed 7 years ago by
|Owner:||changed from nobody to findepi|
|Status:||new → assigned|