summaryrefslogtreecommitdiff
path: root/tests/delete
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2020-08-30 03:00:15 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-08-31 08:11:28 +0200
commitf6405c0b8ef7aff513b105c1da68407a881a3671 (patch)
tree32aa7b4a94f0029a32b28b87d8fc2ca8cc26b7be /tests/delete
parent38fce49c82858d2cac810679d1c0e518840a8615 (diff)
downloaddjango-f6405c0b8ef7aff513b105c1da68407a881a3671.tar.gz
Fixed #31965 -- Adjusted multi-table fast-deletion on MySQL/MariaDB.
The optimization introduced in 7acef095d73 did not properly handle deletion involving filters against aggregate annotations. It initially was surfaced by a MariaDB test failure but misattributed to an undocumented change in behavior that resulted in the systemic generation of poorly performing database queries in 5b83bae031. Thanks Anton Plotkin for the report. Refs #23576.
Diffstat (limited to 'tests/delete')
-rw-r--r--tests/delete/models.py2
-rw-r--r--tests/delete/tests.py13
2 files changed, 14 insertions, 1 deletions
diff --git a/tests/delete/models.py b/tests/delete/models.py
index 7f6e6a89ce..96ef65c766 100644
--- a/tests/delete/models.py
+++ b/tests/delete/models.py
@@ -141,7 +141,7 @@ class Base(models.Model):
class RelToBase(models.Model):
- base = models.ForeignKey(Base, models.DO_NOTHING)
+ base = models.ForeignKey(Base, models.DO_NOTHING, related_name='rels')
class Origin(models.Model):
diff --git a/tests/delete/tests.py b/tests/delete/tests.py
index e1ec26bc1a..485ae1aaf5 100644
--- a/tests/delete/tests.py
+++ b/tests/delete/tests.py
@@ -709,3 +709,16 @@ class FastDeleteTests(TestCase):
referer = Referrer.objects.create(origin=origin, unique_field=42)
with self.assertNumQueries(2):
referer.delete()
+
+ def test_fast_delete_aggregation(self):
+ # Fast-deleting when filtering against an aggregation result in
+ # a single query containing a subquery.
+ Base.objects.create()
+ with self.assertNumQueries(1):
+ self.assertEqual(
+ Base.objects.annotate(
+ rels_count=models.Count('rels'),
+ ).filter(rels_count=0).delete(),
+ (1, {'delete.Base': 1}),
+ )
+ self.assertIs(Base.objects.exists(), False)