#26523: Unable to prefetch the implicit one to one relationship
----------------------------------------------+--------------------
     Reporter:  czpython                      |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.8
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 Given the following models:


 {{{
 class Fruit(models.Model):
     weight = models.PositiveIntegerField(default=100)


 class Apple(Fruit):
     farmer = models.CharField(max_length=200, blank=True)


 class Kiwi(models.Model):
     fruit = models.OneToOneField(Fruit)
     farmer = models.CharField(max_length=200, blank=True)
 }}}

 I would like to prefetch the fruit object related to an apple or kiwi.

 So I ran the following:


 {{{
 In [2]: kiwi = Kiwi.objects.select_related('fruit').latest('id')
 QUERY = u'SELECT "fruits_kiwi"."id", "fruits_kiwi"."fruit_id",
 "fruits_kiwi"."farmer", "fruits_fruit"."id", "fruits_fruit"."weight" FROM
 "fruits_kiwi" INNER JOIN "fruits_fruit" ON ( "fruits_kiwi"."fruit_id" =
 "fruits_fruit"."id" ) ORDER BY "fruits_kiwi"."id" DESC LIMIT 1' - PARAMS =
 () [0.84ms]

 In [3]: kiwi.fruit
 Out[3]: <Fruit: Fruit #00021>
 }}}

 As you can see, calling kiwi.fruit does not result in any extra queries.

 Now I tried with Apple:


 {{{
 In [4]: apple = Apple.objects.select_related('fruit_ptr').latest('id')
 QUERY = u'SELECT "fruits_fruit"."id", "fruits_fruit"."weight",
 "fruits_apple"."fruit_ptr_id", "fruits_apple"."farmer" FROM "fruits_apple"
 INNER JOIN "fruits_fruit" ON ( "fruits_apple"."fruit_ptr_id" =
 "fruits_fruit"."id" ) ORDER BY "fruits_apple"."fruit_ptr_id" DESC LIMIT 1'
 - PARAMS = () [0.31ms]

 In [5]: apple.fruit_ptr
 QUERY = u'SELECT "fruits_fruit"."id", "fruits_fruit"."weight" FROM
 "fruits_fruit" WHERE "fruits_fruit"."id" = %s' - PARAMS = (16,) [0.35ms]
 Out[5]: <Fruit: Fruit #00016>
 }}}

 Calling apple.fruit_ptr results in a query to the database even though
 it's set to prefetch the related fruit.

 I've traced the bug to
 
https://github.com/django/django/blob/stable/1.8.x/django/db/models/query_utils.py#L186

--
Ticket URL: <https://code.djangoproject.com/ticket/26523>
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/051.a0c46c16f906c9048732efcece93ffae%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to