Opened 10 years ago
Closed 9 years ago
#23791 closed Bug (fixed)
Subqueries with non-"id" OneToOneField primary keys try to join against the wrong column name
Reported by: | ris | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | orm primary_key OneToOneField subquery values |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
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.
Attachments (1)
Change History (6)
comment:1 by , 10 years ago
Description: | modified (diff) |
---|
comment:3 by , 10 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Version: | 1.7 → master |
I can reproduce using the attached test. Doesn't appear to be a recent regression though (I tested back to 1.5).
by , 10 years ago
Attachment: | 23791-test.diff added |
---|
comment:4 by , 9 years ago
Has patch: | set |
---|---|
Triage Stage: | Accepted → Ready for checkin |
PR from Anssi.
Note:
See TracTickets
for help on using tickets.
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