Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#29230 closed Bug (fixed)

Incorrect behavior of QuerySet.prefetch_related() in some circumstances during multilevel data prefetching

Reported by: Alex Sichkar Owned by: Paulo
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords: prefetch_related
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


In some circumstances prefetched data might be overwritten.

IMHO this ticket is related to the issue:

If I revert changes from this pull request:
there is no such problem.

Prepared sample project with test case to reproduce the issue.

Attachments (1) (8.2 KB) - added by Alex Sichkar 5 years ago.

Download all attachments as: .zip

Change History (6)

Changed 5 years ago by Alex Sichkar

Attachment: added

comment:1 Changed 5 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

I found that your test case was fixed in Django 2.0 by 379caf397ea41923278821085204c296f960e70e. Since a regression test wasn't added as part of that commit, I'll accept the ticket to do that. If you can adapt your test for tests/prefetch_related (using existing models as much as possible) that would be great.

comment:2 Changed 5 years ago by Paulo

Owner: changed from nobody to Paulo
Status: newassigned

comment:3 Changed 5 years ago by Paulo


The commit in didn't fix this.
It just reversed the execution order of prefetch objects.

Changing the attached test from:

projects = Project.objects.prefetch_related(
    Prefetch('tasks', queryset=tasks),
    Prefetch('tasks', to_attr='some_attr_name'),


projects = Project.objects.prefetch_related(
    Prefetch('tasks', to_attr='some_attr_name'),
    Prefetch('tasks', queryset=tasks),

Made it fail again.

comment:4 Changed 5 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 6104875a:

Fixed #29230 -- Fixed nested prefetches that clash with descriptors.

comment:5 Changed 5 years ago by Tim Graham <timograham@…>

In 25d4d846:

[2.1.x] Fixed #29230 -- Fixed nested prefetches that clash with descriptors.

Backport of 6104875a2cc797bbd1254aa61f22a9b03d652128 from master

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