select_related returns incorrect results when one model has two ForeignKeys to the same model
|Reported by:||ElliottM||Owned by:||Jacob|
|Severity:||Keywords:||qsrf-cleanup select_related foreign key|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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:
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.
Change History (14)
comment:1 Changed 9 years ago by
|Owner:||changed from nobody to Malcolm Tredinnick|
|Patch needs improvement:||unset|
comment:2 Changed 9 years ago by
|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 Changed 9 years ago by
|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 Changed 9 years ago by
|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:6 Changed 8 years ago by
|Owner:||changed from Malcolm Tredinnick to Jacob|
|Status:||new → assigned|