Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#26667 closed Bug (fixed)

Invalid query for OneToOneField if 'to_field' specified and primary_key=True

Reported by: Vitaliy Yelnik Owned by: nobody
Component: Database layer (models, ORM) Version: 1.9
Severity: Release blocker Keywords: OneToOneField to_field primary_key
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Vitaliy Yelnik)

Given the following models:

class Model1(models.Model):
    str_id = models.CharField(max_length=12, unique=True)


class Model2(models.Model):
    model1 = models.OneToOneField(Model1, to_field='str_id', primary_key=True)

In Django 1.9 query:

Model2.objects.filter(model1=instance_model1)

always returns an empty list, though in Django 1.8 works

Django 1.9 query

In [4]: print Model2.objects.filter(model1=instance_model1).query
SELECT "app_model2"."model1_id" FROM "app_model2" WHERE "app_model2"."model1_id" = 1

Django 1.8 query

In [4]: print Model2.objects.filter(model1=instance_model1).query
SELECT `app_model2`.`model1_id` FROM `app_model2` WHERE `app_model2`.`model1_id` = 1234567890ax

Attachments (1)

26667-test.diff (1.9 KB ) - added by Tim Graham 8 years ago.
A regression test for Django's test suite

Download all attachments as: .zip

Change History (7)

comment:1 by Vitaliy Yelnik, 8 years ago

Description: modified (diff)

comment:2 by Simon Charette, 8 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

by Tim Graham, 8 years ago

Attachment: 26667-test.diff added

A regression test for Django's test suite

comment:3 by Tim Graham, 8 years ago

Has patch: set

comment:4 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: newclosed

In e2296e7f:

Fixed #26667 -- Fixed a regression in queries on a OneToOneField that has to_field and primary_key=True.

Thanks Simon Charette for review.

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

In d5d8d35e:

[1.9.x] Fixed #26667 -- Fixed a regression in queries on a OneToOneField that has to_field and primary_key=True.

Thanks Simon Charette for review.

Backport of e2296e7f0acf9c50868ab997ba565c1f47beb45b from master

comment:6 by Tim Graham <timograham@…>, 8 years ago

In 8996054:

[1.10.x] Fixed #26667 -- Fixed a regression in queries on a OneToOneField that has to_field and primary_key=True.

Thanks Simon Charette for review.

Backport of e2296e7f0acf9c50868ab997ba565c1f47beb45b from master

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