mirror of
https://github.com/django/django.git
synced 2025-01-03 15:06:09 +00:00
Fixed #30330 -- Fixed setting of primary key to None during fast-delete.
Regression in bc7dd8490b
.
This commit is contained in:
parent
19fc6376ce
commit
afc708cf6d
@ -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):
|
||||||
|
@ -22,3 +22,6 @@ Bugfixes
|
|||||||
:class:`~django.contrib.postgres.aggregates.ArrayAgg` and
|
:class:`~django.contrib.postgres.aggregates.ArrayAgg` and
|
||||||
:class:`~django.contrib.postgres.aggregates.StringAgg` when it contains an
|
:class:`~django.contrib.postgres.aggregates.StringAgg` when it contains an
|
||||||
expression with params (:ticket:`30332`).
|
expression with params (:ticket:`30332`).
|
||||||
|
|
||||||
|
* Fixed a regression in Django 2.2 that caused a single instance fast-delete
|
||||||
|
to not set the primary key to ``None`` (:ticket:`30330`).
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from math import ceil
|
from math import ceil
|
||||||
|
|
||||||
from django.db import IntegrityError, connection, models
|
from django.db import IntegrityError, connection, models
|
||||||
|
from django.db.models.deletion import Collector
|
||||||
from django.db.models.sql.constants import GET_ITERATOR_CHUNK_SIZE
|
from django.db.models.sql.constants import GET_ITERATOR_CHUNK_SIZE
|
||||||
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
|
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
|
||||||
|
|
||||||
@ -471,6 +472,14 @@ class FastDeleteTests(TestCase):
|
|||||||
self.assertEqual(User.objects.count(), 1)
|
self.assertEqual(User.objects.count(), 1)
|
||||||
self.assertTrue(User.objects.filter(pk=u2.pk).exists())
|
self.assertTrue(User.objects.filter(pk=u2.pk).exists())
|
||||||
|
|
||||||
|
def test_fast_delete_instance_set_pk_none(self):
|
||||||
|
u = User.objects.create()
|
||||||
|
# User can be fast-deleted.
|
||||||
|
collector = Collector(using='default')
|
||||||
|
self.assertTrue(collector.can_fast_delete(u))
|
||||||
|
u.delete()
|
||||||
|
self.assertIsNone(u.pk)
|
||||||
|
|
||||||
def test_fast_delete_joined_qs(self):
|
def test_fast_delete_joined_qs(self):
|
||||||
a = Avatar.objects.create(desc='a')
|
a = Avatar.objects.create(desc='a')
|
||||||
User.objects.create(avatar=a)
|
User.objects.create(avatar=a)
|
||||||
|
Loading…
Reference in New Issue
Block a user