mirror of
https://github.com/django/django.git
synced 2025-11-18 19:01:40 +00:00
Fix PK not cleared after fast delete on models with no dependencies
Previously, deleting a model instance with no dependencies did not reset its primary key to None. This ensures that after a .delete() call, the instance's PK is cleared, matching expected behavior. Adds a test case for models using the fast delete path.
This commit is contained in:
parent
19fc6376ce
commit
53cb338afb
3 changed files with 19 additions and 1 deletions
|
|
@ -277,6 +277,7 @@ class Collector:
|
||||||
if self.can_fast_delete(instance):
|
if self.can_fast_delete(instance):
|
||||||
with transaction.mark_for_rollback_on_error():
|
with transaction.mark_for_rollback_on_error():
|
||||||
count = sql.DeleteQuery(model).delete_batch([instance.pk], self.using)
|
count = sql.DeleteQuery(model).delete_batch([instance.pk], self.using)
|
||||||
|
setattr(instance, model._meta.pk.attname, None)
|
||||||
return count, {model._meta.label: count}
|
return count, {model._meta.label: count}
|
||||||
|
|
||||||
with transaction.atomic(using=self.using, savepoint=False):
|
with transaction.atomic(using=self.using, savepoint=False):
|
||||||
|
|
|
||||||
|
|
@ -126,3 +126,8 @@ class Base(models.Model):
|
||||||
|
|
||||||
class RelToBase(models.Model):
|
class RelToBase(models.Model):
|
||||||
base = models.ForeignKey(Base, models.DO_NOTHING)
|
base = models.ForeignKey(Base, models.DO_NOTHING)
|
||||||
|
|
||||||
|
|
||||||
|
class Origin(models.Model):
|
||||||
|
"""Model with no reverse relations and no dependencies."""
|
||||||
|
name = models.CharField(max_length=100)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
|
||||||
|
|
||||||
from .models import (
|
from .models import (
|
||||||
MR, A, Avatar, Base, Child, HiddenUser, HiddenUserProfile, M, M2MFrom,
|
MR, A, Avatar, Base, Child, HiddenUser, HiddenUserProfile, M, M2MFrom,
|
||||||
M2MTo, MRNull, Parent, R, RChild, S, T, User, create_a, get_default_r,
|
M2MTo, MRNull, Origin, Parent, R, RChild, S, T, User, create_a, get_default_r,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -522,3 +522,15 @@ class FastDeleteTests(TestCase):
|
||||||
User.objects.filter(avatar__desc='missing').delete(),
|
User.objects.filter(avatar__desc='missing').delete(),
|
||||||
(0, {'delete.User': 0})
|
(0, {'delete.User': 0})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_fast_delete_instance_set_pk_to_none(self):
|
||||||
|
"""
|
||||||
|
Test that deleting an instance with no dependencies clears its PK.
|
||||||
|
"""
|
||||||
|
# Origin model has no dependencies, should use fast delete path
|
||||||
|
origin = Origin.objects.create(name='test')
|
||||||
|
origin_pk = origin.pk
|
||||||
|
self.assertIsNotNone(origin_pk)
|
||||||
|
origin.delete()
|
||||||
|
# After delete, pk should be None
|
||||||
|
self.assertIsNone(origin.pk)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue