[Django] #29230: Incorrect behavior of QuerySet.prefetch_related() in some circumstances during multilevel data prefetching

22 views
Skip to first unread message

Django

unread,
Mar 16, 2018, 4:05:49 PM3/16/18
to django-...@googlegroups.com
#29230: Incorrect behavior of QuerySet.prefetch_related() in some circumstances
during multilevel data prefetching
-------------------------------------+-------------------------------------
Reporter: Alex | Owner: nobody
Sichkar |
Type: Bug | Status: new
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords: prefetch_related
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
In some circumstances prefetched data might be overwritten.

IMHO this ticket is related to the issue:
https://code.djangoproject.com/ticket/24873

If I revert changes from this pull request:
https://github.com/django/django/pull/4723
there is no such problem.

Prepared sample project with test case to reproduce the issue.

--
Ticket URL: <https://code.djangoproject.com/ticket/29230>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 16, 2018, 4:06:10 PM3/16/18
to django-...@googlegroups.com
#29230: Incorrect behavior of QuerySet.prefetch_related() in some circumstances
during multilevel data prefetching
-------------------------------------+-------------------------------------
Reporter: Alex Sichkar | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Alex Sichkar):

* Attachment "prefetch_related_issue.zip" added.

Django

unread,
Mar 19, 2018, 9:03:42 AM3/19/18
to django-...@googlegroups.com
#29230: Incorrect behavior of QuerySet.prefetch_related() in some circumstances
during multilevel data prefetching
-------------------------------------+-------------------------------------
Reporter: Alex Sichkar | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29230#comment:1>

Django

unread,
May 16, 2018, 9:55:55 PM5/16/18
to django-...@googlegroups.com
#29230: Incorrect behavior of QuerySet.prefetch_related() in some circumstances
during multilevel data prefetching
-------------------------------------+-------------------------------------
Reporter: Alex Sichkar | Owner: Paulo
Type: Bug | Status: assigned

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Paulo):

* owner: nobody => Paulo
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/29230#comment:2>

Django

unread,
May 27, 2018, 7:54:50 AM5/27/18
to django-...@googlegroups.com
#29230: Incorrect behavior of QuerySet.prefetch_related() in some circumstances
during multilevel data prefetching
-------------------------------------+-------------------------------------
Reporter: Alex Sichkar | Owner: Paulo
Type: Bug | Status: assigned
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Paulo):

PR: https://github.com/django/django/pull/9995

The commit in
https://code.djangoproject.com/changeset/379caf397ea41923278821085204c296f960e70e/
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'),
)
}}}


to:


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


Made it fail again.

--
Ticket URL: <https://code.djangoproject.com/ticket/29230#comment:3>

Django

unread,
May 27, 2018, 10:10:47 PM5/27/18
to django-...@googlegroups.com
#29230: Incorrect behavior of QuerySet.prefetch_related() in some circumstances
during multilevel data prefetching
-------------------------------------+-------------------------------------
Reporter: Alex Sichkar | Owner: Paulo
Type: Bug | Status: closed

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: prefetch_related | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"6104875a2cc797bbd1254aa61f22a9b03d652128" 6104875a]:
{{{
#!CommitTicketReference repository=""
revision="6104875a2cc797bbd1254aa61f22a9b03d652128"
Fixed #29230 -- Fixed nested prefetches that clash with descriptors.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29230#comment:4>

Django

unread,
May 27, 2018, 10:15:26 PM5/27/18
to django-...@googlegroups.com
#29230: Incorrect behavior of QuerySet.prefetch_related() in some circumstances
during multilevel data prefetching
-------------------------------------+-------------------------------------
Reporter: Alex Sichkar | Owner: Paulo
Type: Bug | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: prefetch_related | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"25d4d8465b5a92cae5647117e2a42f71cd97a06f" 25d4d846]:
{{{
#!CommitTicketReference repository=""
revision="25d4d8465b5a92cae5647117e2a42f71cd97a06f"
[2.1.x] Fixed #29230 -- Fixed nested prefetches that clash with
descriptors.

Backport of 6104875a2cc797bbd1254aa61f22a9b03d652128 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29230#comment:5>

Reply all
Reply to author
Forward
0 new messages