#7110 closed (fixed)
select_related returns incorrect results when one model has two ForeignKeys to the same model
Reported by: | ElliottM | Owned by: | Jacob |
---|---|---|---|
Component: | Uncategorized | Version: | dev |
Severity: | Keywords: | qsrf-cleanup select_related foreign key | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This started occurring just after the qs-rf branch was merged into trunk.
Essentially, my Connection models has two separate FKs to the Port model (called start and end), the Port model has a FK to the device model, and the Device model has a FK to the building model.
When I want to get a list of all connections that start and end in the same building, I do the following:
Connection.objects.filter(start__device__building=building_name, end__device__building=building_name)
This works correctly for the most part. However, if I add .select_related() onto the end of the line, it all goes wrong. The following is the output from runtest.py in the attachment, which should show the problem:
>>> from test import runtest Begin list of normal filter 10 FROM router/4 TO switch/7 11 FROM switch/7 TO server/1 (end list) Begin list of filter using select_related 10 FROM switch/4 TO switch/7 11 FROM server/7 TO server/1 (end list)
As you can see, in the first list, the start of the first connection is device "router", port 4 and the end is device "switch", port 7. When the exact same query is done with select_related on the end, the port numbers are the same, but the start device has for some reason been changed to be equal to the end device. From what I read in the documentation, select_related should not affect the output at all, so I believe this must be a bug.
Attached is my whole test app where you can see for yourself what is happening.
Attachments (1)
Change History (14)
by , 17 years ago
Attachment: | select_related_test.zip added |
---|
comment:1 by , 17 years ago
Owner: | changed from | to
---|
comment:2 by , 17 years ago
Summary: | select_related returns incorrect results in certain cases. → select_related returns incorrect results when one models has two FKS to the same model |
---|
comment:3 by , 17 years ago
Summary: | select_related returns incorrect results when one models has two FKS to the same model → select_related returns incorrect results when one models has two ForeignKeys to the same model |
---|
comment:4 by , 17 years ago
Summary: | select_related returns incorrect results when one models has two ForeignKeys to the same model → select_related returns incorrect results when one model has two ForeignKeys to the same model |
---|
comment:5 by , 16 years ago
Triage Stage: | Unreviewed → Accepted |
---|
See also #7125; I suspect this is the same bug but I'm not sure so I'm not going to mark either a duplicate, yet.
comment:6 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:8 by , 16 years ago
Keywords: | qsrf-cleanup added |
---|
comment:9 by , 16 years ago
milestone: | → 1.0 |
---|
comment:10 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:11 by , 16 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
The problem still exists, and can be triggered using a more complicated model like the one in #8106
comment:12 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
#8106 is slighty different, actually, so let's take that discussion over there.
App containing models.py and "runtest.py" which shows a test case.