Opened 7 years ago

Closed 7 years ago

#6761 closed (fixed)

select_related broken with model inheritance

Reported by: dcwatson Owned by: nobody
Component: Database layer (models, ORM) Version: queryset-refactor
Severity: Keywords: qs-rf model inheritance select_related
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation:
Needs tests: Patch needs improvement:
Easy pickings: UI/UX:

Description

When using select_related() on an inherited model, it tries (and fails, obviously) to select parent fields on the child table. For instance, using the models from #6706, calling Employer.objects.select_related() yields the following SQL:

SELECT "testapp_employer"."id", "testapp_employer"."first_name", "testapp_employer"."last_name",
"testapp_employer"."personinfo_ptr_id", "testapp_employer"."title", "testapp_employer"."office_id",
"testapp_personinfo"."id", "testapp_personinfo"."first_name", "testapp_personinfo"."last_name",
"testapp_location"."id", "testapp_location"."name" FROM "testapp_employer"
INNER JOIN "testapp_personinfo" ON ("testapp_employer"."personinfo_ptr_id" = "testapp_personinfo"."id")
INNER JOIN "testapp_location" ON ("testapp_employer"."office_id" = "testapp_location"."id")
ORDER BY "testapp_personinfo"."last_name" ASC, "testapp_personinfo"."first_name" ASC

I think the solution may involve using opts.local_fields in django.db.models.sql.query.fill_related_selections, but I don't know the code well enough to be certain.

Change History (2)

comment:1 Changed 7 years ago by mtredinnick

(In [7240]) queryset-refactor: Fixed default (no fields) case of select_related() to work with model inheritance. Refs #6761.

comment:2 Changed 7 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

(In [7241]) queryset-refactor: Second part of select_related() fix.

Relations on the parent model can now be specified as part of the fields list.
Fixed #6761.

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