1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #33759 -- Avoided unnecessary subquery in QuerySet.delete() with self-referential subqueries if supported.

This commit is contained in:
4the4ryushin
2023-04-28 02:33:25 +05:30
committed by Mariusz Felisiak
parent 5a6d4d3bfd
commit 0b0998dc15
4 changed files with 21 additions and 9 deletions

View File

@@ -379,15 +379,20 @@ class DeleteTests(TestCase):
child = Child.objects.create(name="Juan")
Book.objects.create(pagecount=500, owner=child)
PlayedWith.objects.create(child=child, toy=toy, date=datetime.date.today())
Book.objects.filter(
Exists(
Book.objects.filter(
pk=OuterRef("pk"),
owner__toys=toy.pk,
),
)
).delete()
with self.assertNumQueries(1) as ctx:
Book.objects.filter(
Exists(
Book.objects.filter(
pk=OuterRef("pk"),
owner__toys=toy.pk,
),
)
).delete()
self.assertIs(Book.objects.exists(), False)
sql = ctx.captured_queries[0]["sql"].lower()
if connection.features.delete_can_self_reference_subquery:
self.assertEqual(sql.count("select"), 1)
class DeleteDistinct(SimpleTestCase):