﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
10790	Too many joins in a comparison for NULL.	Malcolm Tredinnick	Anssi Kääriäinen	"Using these models:
{{{
#!python
class Student(models.Model):
    name = models.CharField(max_length=50)

class Message(models.Model):
    title = models.CharField(max_length=50)
    student = models.ForeignKey(Student, null=True)
}}}
this query generates slightly inefficient SQL:
{{{
#!python
Message.objects.filter(student=None)
}}}
We get this
{{{
#!sql
SELECT `outer_message`.`id`, `outer_message`.`title`, `outer_message`.`student_id`
FROM `outer_message` 
   LEFT OUTER JOIN `outer_student` 
      ON (`outer_message`.`student_id` = `outer_student`.`id`)
WHERE `outer_student`.`id` IS NULL
}}}
when we should ideally be using this:
{{{
#!sql
SELECT `outer_message`.`id`, `outer_message`.`title`, `outer_message`.`student_id`
FROM `outer_message`
WHERE `outer_message`.`student_id` IS NULL
}}}

Not worth worrying about for 1.1, since the result isn't incorrect; just less efficient than it could be.

Note to self for when fixing this: This is essentially the only situation where we can trim a left outer join from the end of a list of joins. We're comparing to NULL and can factor that back across the outer join. Needs special casing in `Query.add_filter()`.
"	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed		wallenfe@… Vlastimil Zíma Dan Watson milosu philippe@… tom@… kmike84@…	Ready for checkin	1	0	0	0	0	0
