-
-
Notifications
You must be signed in to change notification settings - Fork 421
feat: Require explicit on_delete for ForeignKeyField #1896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
feat: Require explicit on_delete for ForeignKeyField #1896
Conversation
This change removes the default CASCADE behavior for the on_delete parameter in ForeignKeyField, making it mandatory. This prevents accidental data loss due to implicit cascading deletes. All tests have been updated to provide an explicit on_delete value. Fixes tortoise#1801
@Hammer2900 Why do you use |
CodSpeed Performance ReportMerging #1896 will not alter performanceComparing Summary
|
I like the change! A few notes:
|
@waketzheng Thanks for the review. I used Furthermore, choosing @henadzit I'll try to add more to the CHANGELOG tonight. |
I will make a PR to aerich after this one merged. |
This change removes the default CASCADE behavior for the `on_delete` parameter in ForeignKeyField, making it mandatory. This prevents accidental data loss due to implicit cascading deletes. Code Changes: - Updated ForeignKeyField definition to require on_delete. - Updated all overloaded function signatures to match the new definition. - Updated tests to provide an explicit on_delete value. Documentation Changes: - Added a dedicated section for the `on_delete` parameter. - Updated examples to include explicit `on_delete` values. Fixes tortoise#1801
@henadzit I've added a separate section with an anchor ( import importlib.metadata as importlib_metadata However, I'm hesitant to include this change in the PR because it might break compatibility with older Python versions (<=3.13) where the external importlib_metadata package is required. I've left the conf.py file unchanged in this PR. |
@Hammer2900 Cloud you fix the conflicts? |
@Hammer2900 can you please also make Sorry for taking so long to review! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR makes the deletion behavior of ForeignKeyField relationships explicit by requiring an explicit on_delete parameter, thereby preventing accidental data loss caused by implicit cascading deletes. The changes update various test and example files to include on_delete=fields.RESTRICT.
Reviewed Changes
Copilot reviewed 35 out of 39 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
tests/model_setup/model_bad_rel2.py | Added on_delete=fields.RESTRICT to the ForeignKeyField in Event model. |
tests/model_setup/model_bad_rel1.py | Added on_delete=fields.RESTRICT to the ForeignKeyField in Event model. |
examples/schema_create.py | Updated the Event model FK to include on_delete=fields.RESTRICT. |
examples/relations_with_unique.py | Updated the Student model FK to include on_delete=fields.RESTRICT. |
examples/relations_recursive.py | Added on_delete=fields.RESTRICT to the recursive FK in Employee model. |
examples/relations.py | Updated the Event model FK to include on_delete=fields.RESTRICT. |
examples/pydantic/tutorial_4.py | Updated the Event model FK to include on_delete=fields.RESTRICT. |
examples/pydantic/tutorial_3.py | Updated the Event model FK to include on_delete=fields.RESTRICT. |
examples/pydantic/recursive.py | Added on_delete=fields.RESTRICT to the recursive FK in Employee model. |
examples/pydantic/early_init.py | Updated the Event model FK to include on_delete=fields.RESTRICT. |
examples/pydantic/basic.py | Updated the Event model FK to include on_delete=fields.RESTRICT. |
examples/group_by.py | Updated the Book model FK (Author relation) to include on_delete=fields.RESTRICT. |
examples/global_table_name_generator.py | Updated the BlogPost model FK to include on_delete=fields.RESTRICT. |
examples/functions.py | Updated the Event model FK to include on_delete=fields.RESTRICT. |
examples/complex_prefetching.py | Updated the Event model FK to include on_delete=fields.RESTRICT. |
examples/complex_filtering.py | Updated the Event model FK to include on_delete=fields.RESTRICT. |
Files not reviewed (4)
- CHANGELOG.rst: Language not supported
- docs/contrib/pydantic.rst: Language not supported
- docs/getting_started.rst: Language not supported
- docs/models.rst: Language not supported
@henadzit Sure. I'll take a look at it this evening. |
@Hammer2900 are you still interested in working on this? If not, no problem, I can pick this up. |
I apologize, but due to circumstances, I can't stay at the computer constantly right now. So, if possible, could someone else finish it up, as there's not much left to do. |
@henadzit There are two PRs about requires on_delete. Cloud you finish this one and then we can close another. |
This change removes the default CASCADE behavior for the on_delete parameter in ForeignKeyField, making it mandatory. This prevents accidental data loss due to implicit cascading deletes. All tests have been updated to provide an explicit on_delete value.
Fixes #1801
Description
This prevents accidental data loss due to implicit cascading deletes, as developers might not be aware of the default CASCADE behavior. By making
on_delete
mandatory, we ensure that the deletion behavior is always explicitly defined.Motivation and Context
The default
on_delete=CASCADE
behavior was identified as a potential source of data loss in issue #1801. Users pointed out that accidental deletions could lead to unintended cascading deletes, especially if developers were not fully aware of the default setting. This change aims to improve data safety by requiring explicit configuration of the deletion behavior.How Has This Been Tested?
Ran the full test suite using
pytest
.Tested on Python 3.13 with SQLite backends.
Checklist: