[Django] #23791: Subqueries with non-"id" OneToOneField primary keys try to join against the wrong column name

15 views
Skip to first unread message

Django

unread,
Nov 10, 2014, 8:49:41 AM11/10/14
to django-...@googlegroups.com
#23791: Subqueries with non-"id" OneToOneField primary keys try to join against the
wrong column name
-------------------------------------+-------------------------------------
Reporter: ris | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) | Keywords: orm primary_key
Severity: Normal | OneToOneField subquery values
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Hi, your favourite ORM obscure-query bug reporter again here...

Using Django 1.7.1 & postgres, example models.py:

{{{
class ModelA ( models.Model ):
x = models.TextField ()

class ModelB ( models.Model ):
a = models.OneToOneField ( ModelA , primary_key = True )
}}}

issuing the query

{{{
>>> ModelC.objects.filter ( pk__in = ModelC.objects.filter ( a__x = "abc"
) )
}}}

results in

{{{
FieldError: Cannot resolve keyword u'id' into field. Choices are: a, a_id
}}}

A workaround exists:

{{{
>>> ModelC.objects.filter ( pk__in = ModelC.objects.filter ( a__x = "abc"
).values ( "pk" ) )
}}}

which behaves correctly.

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

Django

unread,
Nov 10, 2014, 8:51:54 AM11/10/14
to django-...@googlegroups.com
#23791: Subqueries with non-"id" OneToOneField primary keys try to join against the
wrong column name
-------------------------------------+-------------------------------------
Reporter: ris | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: orm primary_key | Unreviewed
OneToOneField subquery values | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by ris):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

> Hi, your favourite ORM obscure-query bug reporter again here...
>
> Using Django 1.7.1 & postgres, example models.py:
>
> {{{
> class ModelA ( models.Model ):
> x = models.TextField ()
>
> class ModelB ( models.Model ):
> a = models.OneToOneField ( ModelA , primary_key = True )
> }}}
>
> issuing the query
>
> {{{
> >>> ModelC.objects.filter ( pk__in = ModelC.objects.filter ( a__x = "abc"
> ) )
> }}}
>
> results in
>
> {{{
> FieldError: Cannot resolve keyword u'id' into field. Choices are: a, a_id
> }}}
>
> A workaround exists:
>
> {{{
> >>> ModelC.objects.filter ( pk__in = ModelC.objects.filter ( a__x = "abc"
> ).values ( "pk" ) )
> }}}
>
> which behaves correctly.

New description:

Hi, your favourite ORM obscure-query bug reporter again here...

Using Django 1.7.1 & postgres, example models.py:

{{{
class ModelA ( models.Model ):
x = models.TextField ()

class ModelB ( models.Model ):
a = models.OneToOneField ( ModelA , primary_key = True )
}}}

issuing the query

{{{
>>> ModelB.objects.filter ( pk__in = ModelB.objects.filter ( a__x = "abc"
) )
}}}

results in

{{{
FieldError: Cannot resolve keyword u'id' into field. Choices are: a, a_id
}}}

A workaround exists:

{{{
>>> ModelB.objects.filter ( pk__in = ModelB.objects.filter ( a__x = "abc"


).values ( "pk" ) )
}}}

which behaves correctly.

--

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

Django

unread,
Nov 10, 2014, 9:59:10 AM11/10/14
to django-...@googlegroups.com
#23791: Subqueries with non-"id" OneToOneField primary keys try to join against the
wrong column name
-------------------------------------+-------------------------------------
Reporter: ris | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: orm primary_key | Unreviewed
OneToOneField subquery values | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by ris):

Having said the workaround behaves correctly, I realize adding .values
(...) screws up the ordering of the subquery, which is important if using
.distinct (...). Similar effects to #23622

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

Django

unread,
Nov 24, 2014, 1:50:47 PM11/24/14
to django-...@googlegroups.com
#23791: Subqueries with non-"id" OneToOneField primary keys try to join against the
wrong column name
-------------------------------------+-------------------------------------
Reporter: ris | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: orm primary_key | Needs documentation: 0
OneToOneField subquery values | Patch needs improvement: 0
Has patch: 0 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timgraham):

* version: 1.7 => master
* stage: Unreviewed => Accepted


Comment:

I can reproduce using the attached test. Doesn't appear to be a recent
regression though (I tested back to 1.5).

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

Django

unread,
Jun 27, 2015, 10:03:22 AM6/27/15
to django-...@googlegroups.com
#23791: Subqueries with non-"id" OneToOneField primary keys try to join against the
wrong column name
-------------------------------------+-------------------------------------
Reporter: ris | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm primary_key | Triage Stage: Ready for
OneToOneField subquery values | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* has_patch: 0 => 1
* stage: Accepted => Ready for checkin


Comment:

[https://github.com/django/django/pull/4918 PR] from Anssi.

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

Django

unread,
Jun 29, 2015, 7:50:19 AM6/29/15
to django-...@googlegroups.com
#23791: Subqueries with non-"id" OneToOneField primary keys try to join against the
wrong column name
-------------------------------------+-------------------------------------
Reporter: ris | Owner: nobody
Type: Bug | Status: closed

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

Keywords: orm primary_key | Triage Stage: Ready for
OneToOneField subquery values | checkin
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: new => closed
* resolution: => fixed


Comment:

In [changeset:"9ed82154bd0bd01c6195942db84302e791ad366f" 9ed8215]:
{{{
#!CommitTicketReference repository=""
revision="9ed82154bd0bd01c6195942db84302e791ad366f"
Fixed #23791 -- Corrected object type check for pk__in=qs

When the pk was a relation field, qs.filter(pk__in=qs) didn't work.

In addition, fixed Restaurant.objects.filter(place=restaurant_instance),
where place is an OneToOneField and the primary key of Restaurant.

A big thank you to Josh for review and to Tim for review and cosmetic
edits.

Thanks to Beauhurst for commissioning the work on this ticket.
}}}

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

Reply all
Reply to author
Forward
0 new messages