feat: deprecate implicit on_delete default in ForeignKeyField, OneToOneField, ManyToManyField#2229
Open
Yad4o wants to merge 1 commit into
Open
feat: deprecate implicit on_delete default in ForeignKeyField, OneToOneField, ManyToManyField#2229Yad4o wants to merge 1 commit into
Yad4o wants to merge 1 commit into
Conversation
Add a DeprecationWarning when `on_delete` is not explicitly passed to `ForeignKeyField`, `OneToOneField`, or `ManyToManyField`. The current silent default of `CASCADE` is dangerous as it can cause unintended cascade deletes. Users should pass `on_delete` explicitly. A sentinel `_UNSET` is used to distinguish "not passed" from a real `CASCADE` value, so existing code continues to work but emits a warning. Fixes tortoise#1801
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1801.
ForeignKeyField,OneToOneField, andManyToManyFieldsilently defaultedon_deletetoCASCADE. This is dangerous — a developer who forgets to seton_deletemay inadvertently cascade-delete data they didn't intend to.This PR adds a
DeprecationWarningwhenon_deleteis omitted, making the implicit behaviour visible and encouraging explicit intent. The default behaviour is preserved for now to avoid a breaking change.Changes
_UNSETsentinel intortoise/fields/relational.pyto detect whenon_deleteis not passedForeignKeyFieldInstance.__init__: emitsDeprecationWarningwhenon_deleteis not explicitly providedOneToOneFieldInstance.__init__: same (delegates toForeignKeyFieldInstance)ManyToManyFieldInstance.__init__: sameForeignKeyField,OneToOneField,ManyToManyField) updated to use the sentinel as default@overloadsignatures are unchanged (type checkers still seeOnDelete = CASCADE) so no type-checking regressionsBefore / After
Notes
on_deletetruly required by removing the sentinel fallback