fix: clear reverse M2M through tables before cascade deletion#14630
Merged
rossops merged 1 commit intoDefectDojo:bugfixfrom Apr 6, 2026
Merged
Conversation
Both bulk_clear_finding_m2m and cascade_delete_related_objects only cleared forward M2M tables (model._meta.many_to_many). Reverse M2M fields defined on other models (Finding_Group.findings, Risk_Acceptance.accepted_findings, EnhancedRiskAcceptance.affected_engagements) were missed, causing FK violations during bulk deletion. Use get_fields() which returns both forward ManyToManyField (via field.remote_field.through) and reverse ManyToManyRel (via field.through) to discover all through tables. Add test covering Finding_Group and Risk_Acceptance reverse M2M cleanup.
Maffooch
approved these changes
Apr 3, 2026
blakeaowens
approved these changes
Apr 4, 2026
rossops
approved these changes
Apr 6, 2026
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
The optimized Async Delete from #14566 correctly cascaded M2M relationships, but not reverse M2M relationships. I could have sworn I tested Finding Groups, but to be sure this PR has test cases for these M2M relationships.
bulk_clear_finding_m2mandcascade_delete_related_objectsonly discovered forward M2M fields (model._meta.many_to_many). Reverse M2M fields defined on other models (Finding_Group.findings,Risk_Acceptance.accepted_findings,EnhancedRiskAcceptance.affected_engagements) were missed.get_fields()which returns both forwardManyToManyField(viafield.remote_field.through) and reverseManyToManyRel(viafield.through) to discover all through tables.Context
Follow-up to #14566. The error observed in production:
Triggered from
async_delete_task→bulk_delete_findingswhen deleting a Product with findings in Finding Groups.