#23270: select_related on fields pointing to subclasses does not work when using
defer
----------------------------------------------+----------------------------
     Reporter:  islavov                       |      Owner:  nobody
         Type:  Uncategorized                 |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.7-rc-2
     Severity:  Normal                        |   Keywords:  select_related
 Triage Stage:  Unreviewed                    |  defer
Easy pickings:  0                             |  Has patch:  0
                                              |      UI/UX:  0
----------------------------------------------+----------------------------
 I have the following models
 {{{
 #!python
 from django.db import models

 class Base(models.Model):
     text = models.TextField()

 class SubClassA(Base):
     name = models.CharField(max_length=32)

 }}}
 In shell
 {{{
 #!python
 In [3]: from defertest.models import SubClassA, Base

 # Select related + defer
 In [4]: c=Base.objects.select_related("subclassa").defer("text")[0]
 (0.001) SELECT "defertest_base"."id", "defertest_subclassa"."base_ptr_id",
 "defertest_subclassa"."name" FROM "defertest_base" LEFT OUTER JOIN
 "defertest_subclassa" ON ("defertest_base"."id" =
 "defertest_subclassa"."base_ptr_id") LIMIT 1; args=()

 # We get an extra query when accessing the subclass
 In [5]: c.subclassa
 (0.000) SELECT "defertest_base"."id", "defertest_base"."text",
 "defertest_subclassa"."base_ptr_id", "defertest_subclassa"."name" FROM
 "defertest_subclassa" INNER JOIN "defertest_base" ON
 ("defertest_subclassa"."base_ptr_id" = "defertest_base"."id") WHERE
 "defertest_subclassa"."base_ptr_id" = 1 ; args=(1,)
 Out[5]: <SubClassA: SubClassA object>

 # If no deferred fields
 In [6]: c=Base.objects.select_related("subclassa")[0]
 (0.000) SELECT "defertest_base"."id", "defertest_base"."text",
 "defertest_subclassa"."base_ptr_id", "defertest_subclassa"."name" FROM
 "defertest_base" LEFT OUTER JOIN "defertest_subclassa" ON
 ("defertest_base"."id" = "defertest_subclassa"."base_ptr_id") LIMIT 1;
 args=()

 # select related works fine ( no extra query )
 In [7]: c.subclassa
 Out[7]: <SubClassA: SubClassA object>
 }}}

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

Reply via email to