Opened 3 years ago

Closed 3 years ago

Last modified 3 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 3 years ago.
A regression test for Django's test suite

Download all attachments as: .zip

Change History (7)

comment:1 Changed 3 years ago by Vitaliy Yelnik

Description: modified (diff)

comment:2 Changed 3 years ago by Simon Charette

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

Changed 3 years ago by Tim Graham

Attachment: 26667-test.diff added

A regression test for Django's test suite

comment:3 Changed 3 years ago by Tim Graham

Has patch: set

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

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 Changed 3 years ago by Tim Graham <timograham@…>

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 Changed 3 years ago by Tim Graham <timograham@…>

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