Code

Opened 3 years ago

Last modified 3 years ago

#15020 new Bug

Redundant joins generated when using reverse foreign key

Reported by: adurdin Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: me@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

A query using a reverse foreign key generates a left outer join to a related table and a second, redundant left outer join to the original table.

Tested in Django 1.1 and trunk r15147, and found to be an issue in both.

Using these models:

from django.db import models

class Original(models.Model):
    pass # Don't need anything more for this test

class Reference(models.Model):
    original = models.ForeignKey(Original, primary_key=True)

Background: the Reference model is used for storing and testing set membership; an Original instance is a member of the set iff its PK is in the Reference table.

Incorrect query was generated for this queryset:

Original.objects.filter(reference__pk__isnull=True)

Generated SQL:

SELECT `fkpk_original`.`id` FROM `fkpk_original`
LEFT OUTER JOIN `fkpk_reference` ON (`fkpk_original`.`id` = `fkpk_reference`.`original_id`)
LEFT OUTER JOIN `fkpk_original` T3 ON (`fkpk_reference`.`original_id` = T3.`id`)
WHERE T3.`id` IS NULL

Expected SQL:

SELECT `fkpk_original`.`id` FROM `fkpk_original`
LEFT OUTER JOIN `fkpk_reference` ON (`fkpk_original`.`id` = `fkpk_reference`.`original_id`)
WHERE `fkpk_reference`.`original_id` IS NULL

Notes

An incorrect query is also generated for the following queryset; however this seems to be covered by #14056:

Original.objects.filter(reference__pk__isnull=False)

Attachments (0)

Change History (6)

comment:1 Changed 3 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to russellm
  • Patch needs improvement unset
  • Status changed from new to assigned
  • Triage Stage changed from Unreviewed to Accepted

I'm not sure I see the connection with #14056, but this certainly appears to be a problem. I'm going to guess that it's the primary_key=True on a ForeignKey that is causing the confusion.

comment:2 Changed 3 years ago by adurdin

To clarify: I found incorrect queries when I was using the two variations on this queryset, ...isnull=True, and ...isnull=False. I presumed that they were related, since it was only a parameter to the queryset that I was changing. I found #14056 when searching to see if this was a reported issue, and found that its description and patch matched and fixed only the second of the two incorrect queries. That could be coincidental; I wasn't able to follow the query generation well enough to know.

comment:3 Changed 3 years ago by russellm

  • Owner changed from russellm to nobody
  • Status changed from assigned to new

Not sure how this got assigned to me

comment:4 Changed 3 years ago by anonymous

  • Severity set to Normal
  • Type set to Bug

comment:5 Changed 2 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:6 Changed 2 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.