Opened 3 years ago

Last modified 10 months ago

#21204 assigned Bug

Query.defer() failure - deferred columns calculated per table instead per alias

Reported by: akaariai Owned by: charettes
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no


In sql.compiler the deferred columns are calculated per table. This doesn't work correctly as it is possible to have the same table multiple times in the query. For example (using models from tests/queries/

        qs = Tag.objects.select_related(
            'parent', 'parent__parent'
            'parent__name', 'parent__parent__category'

Currently this causes both name and category to be deferred for the base model, its parent, and parent's parent. The correct interpretation is that for base model all columns are loaded, for parent category is loaded, name is deferred and for parent's parent name is loaded and category is deferred.

The proper solution is to key the only_load dictionary by alias instead of db_table. This doesn't look to be particularly easy case to solve, as both only_load's construction and usage are somewhat complex.

The bug has likely existed for as long as defer has existed. I'll mark this as master as I don't see a reason why this must be backpatched. The user-visible result of this bug is that more queries than expected are executed in some rare situations, so this isn't too severe.

Failing test case at:

Change History (2)

comment:1 Changed 22 months ago by charettes

  • Has patch set
  • Owner changed from nobody to charettes
  • Patch needs improvement set
  • Status changed from new to assigned

I started working on this here.

comment:2 Changed 10 months ago by timgraham

While the patch has gone quite stale, the test there still fails as of c6c00fbfbb659de4beaad3c612c271ac74f892a7.

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