-
-
Notifications
You must be signed in to change notification settings - Fork 33k
Fixed #27272 -- Added an on_delete RESTRICT handler to allow cascading deletions while protecting direct ones. #7364
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
Conversation
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.
The admin has some custom delete stuff. Did you check that nothing is needed there? (django/contrib/admin/utils.py
). Please uncheck "Patch needs improvement" on the ticket after updating the PR.
The |
For Still need to fix the documentation build errors, and add another test case for the above potential problem about recursive calls not raising |
@izquierdo, do you plan to return to this soon or should we close it? |
I'm sorry for temporarily suspending work on this, I will return to it soon. Please do not close. |
7903a39
to
cc1d0f4
Compare
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.
I haven't tried this out, but I ran into the need for this today. I've added a few initial comments here.
I'd also like to see tests to ensure that this works with more complex situations, e.g.
C R
A ─── B1 ─── X
│ │
└──── B2 ────┘
C C
(This is like having an extra model between Artist
and Song
.)
Thank you for the review. Working on the extra requested tests and other comments, and will also rebase this on top of the latest. |
Gentle reminder @izquierdo, will you be picking this one up again? |
4edb147
to
b3d4da6
Compare
just 2 flake8 errors |
c681d22
to
77d19a8
Compare
I believe all comments have been addressed now.
Changed to |
@izquierdo I don't think that discussion is necessary. Please uncheck "Patch needs improvement" flag if you believe that all comments have been addressed. |
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.
@izquierdo Thanks for this patch 👍 I reviewed docs and left some comments. I'm going to review the rest of this patch, asap.
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.
@izquierdo Thanks again for this patch 👍 We're quite close. Some tests are missing, please check my comments.
Thank you for the review, added tests and addressed comments |
Thanks for updates 👍 Can you check tests failures on MySQL and MariaDB? ( |
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.
@izquierdo Thanks for updates 👍
I rebased from master, pushed minor edits, simplified tests in admin_views
, moved adding add_dependency()
to a separate commit. I'm still working on delete
tests.
I left comments about add_dependency()
. Can you take a look?
Please don't use force-push.
I'm extremely sorry, I rebased on master and completely missed this comment before force-pushing. Please push again and I'll handle the comments from there. |
Recovered your commit and did one final force push to get back there |
@izquierdo Can you take a look at my comments? ☝️ |
Addressed comments |
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.
@izquierdo Thanks for updates 👍 I squashed commit and push minor fixes to tests. We're quite close but I have few questions/concerns:
test_restrict_indirect_cascade
Do we need this? I'm not sure how this path is different fromtest_restrict_path_cascade_indirect
.test_restrict_generic_foreign_key_and_no_fast_delete
Can we test the same withParentA
? i.e. without the second structure withGenericRelation
's.- test fail when I add
ParentA
on MySQL/MariaDB:django.db.utils.IntegrityError: (1451, 'Cannot delete ....
- Do we know why we need a special workaround for MySQL/MariaDB? maybe it's related with the fact that constraints checks cannot be deferred, in this case we should use
connections[using].features.can_defer_constraint_checks
. Otherwise, we probably need another database feature.
Reminder: please don't use force-push it will be easier to review amendments.
The main difference is that there's an extra model between the object from which deletion started and the restricted object, instead of a single model with two instances. This was added as requested earlier. Other than that the diamond structure is the same. I do believe it's better to test both situations but this can be removed if you disagree. Another difference is that |
This happens because of a missing dependency and is also fixed by b1a586f |
This comment suggests that the sorting is indeed because constraint checks cannot be deferred: django/django/db/models/deletion.py Lines 298 to 300 in 4527d5d
But it's not just a workaround for that -- it's also used to dispatch signals in the right order. If you apply the following patch to diff --git a/django/db/models/deletion.py b/django/db/models/deletion.py
index 62afff1f50..6fa9e53bdc 100644
--- a/django/db/models/deletion.py
+++ b/django/db/models/deletion.py
@@ -298,7 +298,8 @@ class Collector:
# if possible, bring the models in an order suitable for databases that
# don't support transactions or cannot defer constraint checks until the
# end of a transaction.
- self.sort()
+ if not connections[self.using].features.can_defer_constraint_checks:
+ self.sort()
# number of objects deleted for each model label
deleted_counter = Counter() then you get failures on Should I update the docstring on |
The latest comments are addressed, please let me know what to do regarding:
Thank you for the review, I appreciate your thoroughness. |
…g deletions while protecting direct ones.
@izquierdo Thanks for your patience and explanations 🥇 . We're ready to merge 🚀 🎉 |
https://code.djangoproject.com/ticket/27272