#26491: Duplicated queries with nested prefetch
----------------------------------------------+--------------------
     Reporter:  duduklein                     |      Owner:  nobody
         Type:  Uncategorized                 |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.9
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 This ticket may be related to #26318 , but it's not the same since it does
 not involve repeated models.

 Consider the following 3 test models:
 {{{
 class Test1(models.Model):
     pass

 class Test2(models.Model):
     test1 = models.ForeignKey(Test1, related_name="tests2")

 class Test3(models.Model):
     test2 = models.ForeignKey(Test2, related_name="tests3")
 }}}

 Through the shell, we create initial entries and run the nested prefetch
 queries. We verify that the last 2 queries are duplicated:
 {{{
 from test.models import Test1, Test2, test3
 from django.db import connection
 from django.db.models import Prefetch

 t1 = Test1.objects.create()
 t2 = Test2.objects.create(test1 = t1)
 t3 = Test3.objects.create(test2 = t2)

 connection.queries_log.clear()

 tests2_prefetch = Prefetch("tests2",
 Test2.objects.prefetch_related("tests3"))
 Test1.objects.prefetch_related(tests2_prefetch)

 assert len(connection.queries) == 4
 assert connection.queries[-1] == connection.queries[-2]
 }}}

 We had 4 queries instead of 3 expected and the last 2 are duplicated (to
 prefetch Test3 entries).

--
Ticket URL: <https://code.djangoproject.com/ticket/26491>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.d3b71c38ebab04c1490bda0b6ef4c816%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to