Opened 9 years ago

Closed 5 months ago

Last modified 5 months ago

#25173 closed Bug (duplicate)

multi-table inheritance: accessing child object after select_related('child') causes extra query

Reported by: Sebastian Illing Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
Severity: Normal Keywords: ORM, select_related, model inheritance
Cc: V.P. Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

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


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.

Attachments (1)

issue25173-reproduction-testcase.diff (1.6 KB ) - added by Baptiste Mispelon 9 years ago.
Reproduction testcase

Download all attachments as: .zip

Change History (8)

comment:1 by Shai Berger, 9 years ago

Could you please verify the following:

  • After
    obj = A.objects.select_related('b', 'field_fk).first()
    You can traverse the FK without incurring another database query;
  • You should be able to save the query by explicitly using the "a" object:
       obj = obj.b        
       print (obj.a_ptr.field_fk)

but I'd agree that, even if it works, that's a workaround for something that's worth solving.

comment:2 by Sebastian Illing, 9 years ago

obj = A.objects.select_related('b', 'field_fk).first()
  • yes there is only one query
   obj = obj.b        
   print (obj.a_ptr.field_fk)
  • this also results in one query.

But unfortunately I can't do that in my actual use case.

comment:3 by Baptiste Mispelon, 9 years ago

Triage Stage: UnreviewedAccepted

As Shai said, it's probably worth looking into fixing this.

Attached is a reproduction testcase for this issue.

by Baptiste Mispelon, 9 years ago

Reproduction testcase

comment:4 by Tim Graham, 9 years ago

Description: modified (diff)
Resolution: duplicate
Status: newclosed
Summary: Model inheritance and select_relatedmulti-table inheritance: accessing child object after select_related('child') causes extra query

Looks like a duplicate of #16043.

comment:5 by V.P., 5 months ago

Cc: V.P. added
Resolution: duplicate
Status: closednew

This ticket is not a duplicate of #16043, and should be reopened.
Ticket #16043 is about accessing parent model from child, this ticket is about the opposite operation.
This problem still exists in Django 3.2, and probably in later versions too.

comment:6 by Mariusz Felisiak, 5 months ago

Resolution: duplicate
Status: newclosed

Please provide a reproducible scenario before reopening a ticket. Tests provided by Baptist pass on the current main branch (and 3.2).

in reply to:  6 comment:7 by V.P., 5 months ago

Mariusz Felisiak, sorry, it looks like I had this behavior because of another issue in code. Now I cannot reproduce it, everything works fine.

Note: See TracTickets for help on using tickets.
Back to Top