[Django] #28375: prefetch_related bug with string primary key.

33 views
Skip to first unread message

Django

unread,
Jul 7, 2017, 4:40:58 PM7/7/17
to django-...@googlegroups.com
#28375: prefetch_related bug with string primary key.
-------------------------------------+-------------------------------------
Reporter: Maks Y | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 1.11
layer (models, ORM) | Keywords: prefetch_related
Severity: Normal | string primary key.
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The issue:
prefetch_related failed if prefetching by char primary key.

Django version 1.11.3
Python 2.7

reproducible steps:
**1) django-admin startproject pk_string**

**2) cd pk_string**

**3) django-admin startapp users**

**4) update users.models**
{{{#!python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

# Create your models here.

class User(models.Model):
email = models.CharField(max_length=255, unique=True)

class UserData(models.Model):
email = models.OneToOneField(User, to_field='email', primary_key=True)
note = models.CharField(max_length=255);
}}}

**5) install app pk_string.settings**
{{{#!python
...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
]
...
}}}

**6) ./manage.py makemigrations**
**7) ./manage.py migrate**
**8) ./manage.py shell**
{{{#!python
from users.models import User, UserData
User.objects.create(email = '111111')
User.objects.create(email = '222222')
User.objects.create(email = '333333')
User.objects.create(email = '444444')

users = User.objects.all().prefetch_related('userdata')
users[0]
>>> "<User: User object>"

UserData.objects.create(email=users[0] , note = '111')
UserData.objects.create(email=users[2] , note = '222')
UserData.objects.create(email=users[3] , note = '333')
users = User.objects.all().prefetch_related('userdata')

>>> users[0]
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py",
line 289, in __getitem__
return list(qs)[0]
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py",
line 250, in __iter__
self._fetch_all()
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py",
line 1120, in _fetch_all
self._prefetch_related_objects()
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py",
line 675, in _prefetch_related_objects
prefetch_related_objects(self._result_cache,
*self._prefetch_related_lookups)
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py",
line 1469, in prefetch_related_objects
obj_list, additional_lookups = prefetch_one_level(obj_list,
prefetcher, lookup, level)
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py",
line 1582, in prefetch_one_level
prefetcher.get_prefetch_queryset(instances,
lookup.get_current_queryset(level)))
File "/usr/local/lib/python2.7/site-
packages/django/db/models/fields/related_descriptors.py", line 362, in
get_prefetch_queryset
instance = instances_dict[rel_obj_attr(rel_obj)]
KeyError: u'111111'
}}}

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

Django

unread,
Jul 7, 2017, 4:42:19 PM7/7/17
to django-...@googlegroups.com
#28375: prefetch_related bug with string primary key.
-------------------------------------+-------------------------------------
Reporter: Maks Y | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage:
string primary key. | Unreviewed
Has patch: 0 | Needs documentation: 0

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

* Attachment "pk_string.tar.gz" added.

Project example source code. So you could skip all steps and begin from
the last step to reproduce the issue.

Django

unread,
Jul 8, 2017, 8:36:30 AM7/8/17
to django-...@googlegroups.com
#28375: QuerySet.prefetch_related() crashes with KeyError if model uses to_field
and string primary key

-------------------------------------+-------------------------------------
Reporter: Maks Y | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage: Accepted
string primary key. |
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


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

Django

unread,
Jul 10, 2017, 6:13:59 PM7/10/17
to django-...@googlegroups.com
#28375: QuerySet.prefetch_related() crashes with KeyError if model uses to_field
and string primary key
-------------------------------------+-------------------------------------
Reporter: Maks Y | Owner: Paulo
Type: Bug | Status: assigned

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage: Accepted
string primary key. |
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/28375#comment:2>

Django

unread,
Aug 5, 2017, 12:37:42 PM8/5/17
to django-...@googlegroups.com
#28375: QuerySet.prefetch_related() crashes with KeyError if model uses to_field
and string primary key
-------------------------------------+-------------------------------------

Reporter: Maks Y | Owner: Paulo
Type: Bug | Status: assigned
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage: Accepted
string primary key. |
Has patch: 0 | Needs documentation: 0

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

Comment (by Paulo):

Hello Tim,
Will a fix for this be backported to 1.11?
If that is the case, then I can make sure to base if off of current master
instead of https://github.com/django/django/pull/6721

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

Django

unread,
Aug 7, 2017, 3:57:56 PM8/7/17
to django-...@googlegroups.com
#28375: QuerySet.prefetch_related() crashes with KeyError if model uses to_field
and string primary key
-------------------------------------+-------------------------------------

Reporter: Maks Y | Owner: Paulo
Type: Bug | Status: assigned
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage: Accepted
string primary key. |
Has patch: 0 | Needs documentation: 0

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

Comment (by Tim Graham):

A fix could be backported if the patch is simple and the risk of
introducing a regression seems low.

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

Django

unread,
Aug 14, 2017, 5:30:17 PM8/14/17
to django-...@googlegroups.com
#28375: QuerySet.prefetch_related() crashes with KeyError if model uses to_field
and string primary key
-------------------------------------+-------------------------------------

Reporter: Maks Y | Owner: Paulo
Type: Bug | Status: assigned
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage: Accepted
string primary key. |
Has patch: 1 | Needs documentation: 0

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

* has_patch: 0 => 1


Comment:

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

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

Django

unread,
Aug 21, 2017, 4:47:52 PM8/21/17
to django-...@googlegroups.com
#28375: QuerySet.prefetch_related() crashes with KeyError if model uses to_field
and string primary key
-------------------------------------+-------------------------------------
Reporter: Maks Y | Owner: Paulo
Type: Bug | Status: closed

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

Keywords: prefetch_related | Triage Stage: Accepted
string primary key. |
Has patch: 1 | 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:"fea9cb46aacc73cabac883a806ccb7fdc1f979dd" fea9cb4]:
{{{
#!CommitTicketReference repository=""
revision="fea9cb46aacc73cabac883a806ccb7fdc1f979dd"
Fixed #28375 -- Fixed KeyError crash on reverse prefetch of a model with
OneToOneField primary key to a non-pk field.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28375#comment:6>

Reply all
Reply to author
Forward
0 new messages