Django

Code

Ticket #7110 (closed: fixed)

Opened 3 months ago

Last modified 3 weeks ago

select_related returns incorrect results when one model has two ForeignKeys to the same model

Reported by: ElliottM Assigned to: jacob
Milestone: 1.0 Component: Uncategorized
Version: SVN Keywords: qsrf-cleanup select_related foreign key
Cc: Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

select_related_test.zip (3.3 kB) - added by ElliottM on 04/28/08 12:29:41.
App containing models.py and "runtest.py" which shows a test case.

Change History

04/28/08 12:29:41 changed by ElliottM

  • attachment select_related_test.zip added.

App containing models.py and "runtest.py" which shows a test case.

04/28/08 19:54:56 changed by mtredinnick

  • owner changed from nobody to mtredinnick.
  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

04/29/08 10:30:36 changed by ElliottM

  • summary changed from select_related returns incorrect results in certain cases. to select_related returns incorrect results when one models has two FKS to the same model.

04/29/08 10:30:59 changed by ElliottM

  • summary changed from select_related returns incorrect results when one models has two FKS to the same model to select_related returns incorrect results when one models has two ForeignKeys to the same model.

04/29/08 10:31:15 changed by ElliottM

  • summary changed from select_related returns incorrect results when one models has two ForeignKeys to the same model to select_related returns incorrect results when one model has two ForeignKeys to the same model.

06/08/08 19:10:07 changed by jacob

  • stage changed from Unreviewed to 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.

06/08/08 19:10:11 changed by jacob

  • owner changed from mtredinnick to jacob.
  • status changed from new to assigned.

06/08/08 20:10:15 changed by oyvind

Pretty sure it is the same bug.

06/10/08 10:59:42 changed by gav

  • keywords changed from select_related foreign key to qsrf-cleanup select_related foreign key.

06/16/08 12:10:45 changed by jacob

  • milestone set to 1.0.

06/28/08 21:36:18 changed by mtredinnick

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [7778]) Fixed handling of multiple fields in a model pointing to the same related model.

Thanks to ElliotM, mk and oyvind for some excellent test cases for this. Fixed #7110, #7125.


Add/Change #7110 (select_related returns incorrect results when one model has two ForeignKeys to the same model)




Change Properties
Action