#25173: multi-table inheritance: accessing child object after
select_related('child') causes extra query
-------------------------------------+-------------------------------------
     Reporter:  illing2005           |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  1.8
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  duplicate
     Keywords:  ORM,                 |             Triage Stage:  Accepted
  select_related, model inheritance  |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by timgraham):

 * status:  new => closed
 * resolution:   => duplicate


Old description:

> I have the following in models and run into strange behavior when i use
> select_related and model inheritance:
>
> Models:
> {{{
> class A(models.Model):
>     field_fk = models.ForeignKey('C')
>
> class B(A):
>     fields_b = models.CharField(max_length=255)
>
> class C(models.Model):
>     field_c = models.CharField(max_length=255)
> }}}
>
> So A has a foreign key to C and B inherits from A. Now I want to query A
> downcast it to B and read the relationship to C. To minimize sql queries
> I use select_related:
>
> {{{
> obj = A.objects.select_related('b', 'field_fk).first()
> obj = obj.b
> print(obj.field_fk)  # this prints "C object"
> }}}
>
> Because I use select_related this should result in just one query. But
> somehow the information is lost during downcasting and I get to sql
> queries:
>
> {{{
> SELECT ••• FROM "base_a" INNER JOIN "base_c" ON
>       ( "base_a"."field_fk_id" = "base_c"."id" ) LEFT OUTER JOIN "base_b"
> ON
>       ( "base_a"."id" = "base_b"."a_ptr_id" ) ORDER BY "base_a"."id" ASC
> LIMIT 1
>
> SELECT ••• FROM "base_c" WHERE "base_c"."id" = 1
> }}}
>
> So in the first query looks fine. But I am surprised that I get a second
> query. Is this a bug in django's ORM or am I doing something wrong?
>
> I also posted this at stackoverflow, but I am quite sure that this is a
> bug.
> http://stackoverflow.com/questions/31628588/django-model-inheritance-and-
> select-related

New description:

 I have the following in models and run into strange behavior when i use
 select_related and model inheritance:

 Models:
 {{{
 class A(models.Model):
     field_fk = models.ForeignKey('C')

 class B(A):
     fields_b = models.CharField(max_length=255)

 class C(models.Model):
     field_c = models.CharField(max_length=255)
 }}}

 So A has a foreign key to C and B inherits from A. Now I want to query A
 downcast it to B and read the relationship to C. To minimize sql queries I
 use select_related:

 {{{
 obj = A.objects.select_related('b', 'field_fk).first()
 obj = obj.b
 print(obj.field_fk)  # this prints "C object"
 }}}

 Because I use select_related this should result in just one query. But
 somehow the information is lost during downcasting and I get two sql
 queries:

 {{{
 SELECT ••• FROM "base_a" INNER JOIN "base_c" ON
       ( "base_a"."field_fk_id" = "base_c"."id" ) LEFT OUTER JOIN "base_b"
 ON
       ( "base_a"."id" = "base_b"."a_ptr_id" ) ORDER BY "base_a"."id" ASC
 LIMIT 1

 SELECT ••• FROM "base_c" WHERE "base_c"."id" = 1
 }}}

 So in the first query looks fine. But I am surprised that I get a second
 query. Is this a bug in django's ORM or am I doing something wrong?

 I also posted this at stackoverflow, but I am quite sure that this is a
 bug.
 http://stackoverflow.com/questions/31628588/django-model-inheritance-and-
 select-related

--

Comment:

 Looks like a duplicate of #16043.

--
Ticket URL: <https://code.djangoproject.com/ticket/25173#comment:4>
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/068.c25b2aa7d1c2d2e76f1550cb6581e8c5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to