Opened 8 years ago

Last modified 8 years ago

#26667 closed Bug

Invalid query for OneToOneField if 'to_field' specified and primary_key=True — at Version 1

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

Change History (1)

comment:1 by Vitaliy Yelnik, 8 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top