#9394: Reverse relation lookups with a multi-table inherited model produces
extraneous queries
---------------------------------------------------+------------------------
          Reporter:  ikelly                        |         Owner:  mtredinnick
            Status:  assigned                      |     Milestone:             
         Component:  Database layer (models, ORM)  |       Version:  SVN        
        Resolution:                                |      Keywords:             
             Stage:  Accepted                      |     Has_patch:  0          
        Needs_docs:  0                             |   Needs_tests:  0          
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Comment (by JasonCreighton):

 I'm running into what I think is the same bug. I'm using 1.0.2. Given
 these models:

 {{{
 class Base(models.Model):
     base_info = models.CharField(max_length=50)

 class Extra(Base):
     extra_info = models.CharField(max_length=50)

 class ExtraRelatedData(models.Model):
     extra = models.ForeignKey(Extra)
     related_data = models.CharField(max_length=50)
 }}}

 And some pre-populated data:

 {{{
 >>> e = Extra(base_info="foo", extra_info="bar")
 >>> e.save()
 >>> ed = ExtraRelatedData(extra=e, related_data="quux")
 >>> ed.save()
 }}}

 If I later run:

 {{{
 >>> e = Extra.objects.get(pk=1)
 >>> ExtraRelatedData.objects.filter(extra=e)
 [<ExtraRelatedData: ExtraRelatedData object>]
 >>> for q in connection.queries: print repr(q)
 ...
 {'time': '0.001', 'sql': u'SELECT
 "inheritance_base"."id", "inheritance_base"."base_info",
 "inheritance_extra"."base_ptr_id", "inheritance_extra"."extra_info"
 FROM "inheritance_extra"
 INNER JOIN "inheritance_base" ON ("inheritance_extra"."base_ptr_id" =
 "inheritance_base"."id")
 WHERE "inheritance_extra"."base_ptr_id" = 1 '}
 {'time': '0.000', 'sql': u'SELECT
 "inheritance_base"."id", "inheritance_base"."base_info"
 FROM "inheritance_base" WHERE "inheritance_base"."id" = 1 '}
 {'time': '0.000', 'sql': u'SELECT
 "inheritance_extrarelateddata"."id",
 "inheritance_extrarelateddata"."extra_id",
 "inheritance_extrarelateddata"."related_data"
 FROM "inheritance_extrarelateddata"
 WHERE "inheritance_extrarelateddata"."extra_id" = 1  LIMIT 21'}
 }}}

 Even though "e" is already loaded, Django loads it again. (To get the pk?)
 I can workaround it like so:

 {{{
 >>> e = Extra.objects.get(pk=1)
 >>> ExtraRelatedData.objects.filter(extra__pk=e.pk)
 [<ExtraRelatedData: ExtraRelatedData object>]
 >>> for q in connection.queries: print repr(q)
 ...
 {'time': '0.001', 'sql': u'SELECT
 "inheritance_base"."id", "inheritance_base"."base_info",
 "inheritance_extra"."base_ptr_id", "inheritance_extra"."extra_info"
 FROM "inheritance_extra"
 INNER JOIN "inheritance_base" ON ("inheritance_extra"."base_ptr_id" =
 "inheritance_base"."id")
 WHERE "inheritance_extra"."base_ptr_id" = 1 '}
 {'time': '0.000', 'sql': u'SELECT
 "inheritance_extrarelateddata"."id",
 "inheritance_extrarelateddata"."extra_id",
 "inheritance_extrarelateddata"."related_data"
 FROM "inheritance_extrarelateddata"
 WHERE "inheritance_extrarelateddata"."extra_id" = 1  LIMIT 21'}
 }}}

 [ SQL reformatted slightly ]

 It seems completely wrong to me that there's a case where
 {{{filter(foo=foo)}}} is not that same as {{{filter(foo__pk=foo.pk)}}}.

 So is this the same bug, or should I open a new ticket?

-- 
Ticket URL: <http://code.djangoproject.com/ticket/9394#comment:5>
Django <http://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 post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to