From 3acbb0a08c1589035d55f88a1060b1740f9402d6 Mon Sep 17 00:00:00 2001 From: Vincent Simonin Date: Mon, 22 Dec 2025 19:19:02 +0100 Subject: [PATCH] Fix on delete cascade entity order (#20949) * Fix on delete cascade entity order Since [#20708](https://github.com/netbox-community/netbox/pull/20708) relation with a on delete RESTRICT are not deleted in the proper order. Then the error `violate not-null constraint` occurs and breaks the delete cascade feature. * Revert unrelated and simplify changes --- netbox/core/signals.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netbox/core/signals.py b/netbox/core/signals.py index 2994aaa41..d918d2389 100644 --- a/netbox/core/signals.py +++ b/netbox/core/signals.py @@ -3,7 +3,7 @@ from threading import local from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ObjectDoesNotExist, ValidationError -from django.db.models import CASCADE +from django.db.models import CASCADE, RESTRICT from django.db.models.fields.reverse_related import ManyToManyRel, ManyToOneRel from django.db.models.signals import m2m_changed, post_migrate, post_save, pre_delete from django.dispatch import receiver, Signal @@ -221,7 +221,7 @@ def handle_deleted_object(sender, instance, **kwargs): obj.snapshot() # Ensure the change record includes the "before" state if type(relation) is ManyToManyRel: getattr(obj, related_field_name).remove(instance) - elif type(relation) is ManyToOneRel and relation.null and relation.on_delete is not CASCADE: + elif type(relation) is ManyToOneRel and relation.null and relation.on_delete not in (CASCADE, RESTRICT): setattr(obj, related_field_name, None) obj.save()