Opened 17 years ago

Closed 17 years ago

#6761 closed (fixed)

select_related broken with model inheritance

Reported by: Dan Watson 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: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 by Malcolm Tredinnick, 17 years ago

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

comment:2 by Malcolm Tredinnick, 17 years ago

Resolution: fixed
Status: newclosed

(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