#21961: ForeignKey.on_delete supports database-level cascading options
-------------------------------------+-------------------------------------
Reporter: Xof | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by carljm):
I think we should consider introducing this feature as a totally separate
kwarg (`on_delete_db`) rather than conflating it with the existing Python-
level `on_delete`. The various edge cases and implicit fallbacks in the
existing proposal worry me, and I think it would be better to be more
explicit and clear about what is happening on the database level vs the
Python level.
Right now the contract of `on_delete` is very simple: it takes a Python
callable which will be called when a delete cascades. There is nothing at
all special about the built-in callables, they are just conveniences for
common cases. The current API proposal complicates that contract
dramatically: now you have an `on_delete` kwarg which sometimes accepts
Python callables but can also accept magical constants which do something
entirely different (and also have an implicit fallback relationship with
one of the built-in callables).
So under my proposal, if you want database-level cascade, you would
specify `on_delete=models.DO_NOTHING, on_delete_db=models.DB_CASCADE` or
similar. Sure this is a bit more verbose, but I think that's worth it for
the gains in clarity and simplicity.
For similar reasons, I feel pretty strongly that we should NOT
automatically fallback from db-cascade to non-db-cascade depending on the
backend capabilities. It introduces too many differences in behavior
depending on db backend. Trying to claim to provide cross-db portability
when we can't really do so consistently is worse than simply not claiming
to provide it at all. DB-level cascade (like many other "advanced" db-
level features) is something you should only take advantage of when you
know your code will be running on a database that supports it, full stop.
Use of `on_delete_db` on a backend that can't support it should be an
error.
--
Ticket URL: <https://code.djangoproject.com/ticket/21961#comment:9>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/061.c24df0a2a674b2edbe08e6ea27d7a920%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.