﻿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
13937	Sub-query (exclude) fails when joining	Yeago		"I have a suprisingly simple-looking query that fails in 1.2 using MySQL:

{{{
   Consumer.objects.filter(pwi__status__name='PWI Approved').exclude(contact__date__range=('2010-05-03','2010-05-04'))
}}}

This produces some suspicious SQL:

{{{

   SELECT * FROM `consumer_consumer` 
   WHERE (`consumer_consumer`.`race` = %s  
      AND NOT ((`consumer_consumer`.`id` 
         IN (
              SELECT U1.`consumer_id` FROM `base_contact` U1 
                 WHERE U1.`date` BETWEEN %s and %s
         ) AND NOT (`consumer_consumer`.`id` IS NULL))))

}}}

Take a look at this last line. I'm not sure what mechanism is adding this line, however, there is some wisdom in it because if this is transposed into the sub-query (""AND NOT UI.consumer_id IS NULL""), we have a perfectly functioning query. I can't imagine why we would want to make sure our model's primary key isn't null. 

the Consumer model is rather boring, the contact model goes:

class Contact(models.Model):
   consumer = models.ForeignKey('consumer.Consumer,null=True,blank=True)
"	Bug	closed	Database layer (models, ORM)	1.2	Normal	fixed			Accepted	0	0	0	0	0	0
