Skip to content

Commit

Permalink
Fixed #31219 -- Fixed object deletion crash for nested protected rela…
Browse files Browse the repository at this point in the history
…ted objects.
  • Loading branch information
matthiask authored and felixxm committed Jan 31, 2020
1 parent 8c0c023 commit 4e8d890
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 1 addition & 4 deletions django/db/models/deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,7 @@ def collect(self, objs, source=None, nullable=False, collect_related=True,
try:
field.remote_field.on_delete(self, field, sub_objs, self.using)
except ProtectedError as error:
key = "'%s.%s'" % (
error.protected_objects[0].__class__.__name__,
field.name,
)
key = "'%s.%s'" % (field.model.__name__, field.name)
protected_objects[key] += error.protected_objects
if protected_objects:
raise ProtectedError(
Expand Down
11 changes: 11 additions & 0 deletions tests/delete/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ def test_protect_multiple(self):
with self.assertRaisesMessage(ProtectedError, msg):
a.protect.delete()

def test_protect_path(self):
a = create_a('protect')
a.protect.p = P.objects.create()
a.protect.save()
msg = (
"Cannot delete some instances of model 'P' because they are "
"referenced through protected foreign keys: 'R.p'."
)
with self.assertRaisesMessage(ProtectedError, msg):
a.protect.p.delete()

def test_do_nothing(self):
# Testing DO_NOTHING is a bit harder: It would raise IntegrityError for a normal model,
# so we connect to pre_delete and set the fk to a known value.
Expand Down

0 comments on commit 4e8d890

Please sign in to comment.