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:
committed by
Mariusz Felisiak
parent
5a6d4d3bfd
commit
0b0998dc15
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user