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.
* 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>
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>
* 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>
* 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>
* 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>