Opened 14 years ago

Closed 13 years ago

Last modified 13 years ago

#14876 closed Bug (fixed)

Q | Q with nullable related fields generates INNER JOIN where it should be LEFT JOIN

Reported by: Simon Percivall Owned by: Tomek Paczkowski
Component: Database layer (models, ORM) Version: 1.2
Severity: Normal Keywords: Q, inner join, nullable, dceu2011
Cc: asendecka@… 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

The following example shows that when ORing Q's Person is LEFT OUTER JOINed and House is erroneously INNER JOINed. At the same time ORing querysets works.

from django.db import models
from django.db.models import Q

class House(models.Model):
    streetname = models.CharField(max_length=255)

class Person(models.Model):
    home = models.ForeignKey(House)

class Rock(models.Model):
    owner = models.ForeignKey(Person, null=True)

# with Q | Q
print Rock.objects.filter(Q(owner__isnull=True) | (Q(owner__home__streetname=''))).query

# with qs | qs
print (Rock.objects.filter(Q(owner__isnull=True)) | Rock.objects.filter(Q(owner__home__streetname=''))).query

This of course means that owner__is_null=True won't work with Q's becaues of the INNER JOIN.

Tested with 1.0.4+, 1.2.3+ and 1.3 trunk@14864

Attachments (2)

14876.diff (1.4 KB ) - added by Tomek Paczkowski 14 years ago.
14876.r16622.diff (1.4 KB ) - added by Johannes Dollinger 13 years ago.
cleanup for r16622

Download all attachments as: .zip

Change History (11)

comment:1 by Simon Percivall, 14 years ago

Summary: (Q) | Q with nullable related fields generates INNER JOIN where it should be LEFT JOINQ | Q with nullable related fields generates INNER JOIN where it should be LEFT JOIN

comment:2 by Russell Keith-Magee, 14 years ago

Triage Stage: UnreviewedAccepted

comment:3 by James Addison, 14 years ago

Severity: Normal
Type: Bug

by Tomek Paczkowski, 14 years ago

Attachment: 14876.diff added

comment:4 by Tomek Paczkowski, 14 years ago

Easy pickings: unset
Keywords: dceu2011 added
Owner: changed from nobody to Tomek Paczkowski
Status: newassigned
UI/UX: unset

After countless hours of debugging, one line fix patch is ready. Test included. Done by me & ethlinn (Aleksandra Sendecka).

comment:5 by Aleksandra Sendecka <asendecka@…>, 14 years ago

Cc: asendecka@… added
Has patch: set

comment:6 by Harro, 14 years ago

Triage Stage: AcceptedReady for checkin

Tested, works perfect.

by Johannes Dollinger, 13 years ago

Attachment: 14876.r16622.diff added

cleanup for r16622

comment:7 by Johannes Dollinger, 13 years ago

I can confirm that this patch also fixes #11052.

comment:8 by Russell Keith-Magee, 13 years ago

Resolution: fixed
Status: assignedclosed

In [16648]:

Fixed #14876 -- Ensure that join promotion works correctly when there are nullable related fields. Thanks to simonpercivall for the report, oinopion and Aleksandra Sendecka for the original patch, and to Malcolm for helping me wrestle the edge cases to the ground.

comment:9 by Russell Keith-Magee, 13 years ago

In [16671]:

[1.3.X] Fixed #14876 -- Ensure that join promotion works correctly when there are nullable related fields. Thanks to simonpercivall for the report, oinopion and Aleksandra Sendecka for the original patch, and to Malcolm for helping me wrestle the edge cases to the ground.

Backport of r16648 from trunk.

Note: See TracTickets for help on using tickets.
Back to Top