Opened 15 years ago
Last modified 13 years ago
#12937 closed
Select related() not working as of Beta — at Initial Version
Reported by: | Yeago | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.2-beta |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Given the following expression:
bmodels.Contact.objects.select_related('IR_contact','consumer_contact','program','consumer').filter(consumer__file=file).order_by('- date') }}} And the following models: {{{ class Contact(models.Model): program = models.ForeignKey('agency.Program') consumer = models.ForeignKey('consumer.Consumer',null=True,blank=True) date = models.DateField('Contact Date') notes = models.TextField() date_added = models.DateTimeField(editable=False) method = models.CharField(max_length=10,choices=choices.CONTACT_METHOD,\ null=True,blank=True) # consumer_contact class Contact(basemodels.Contact): base = models.OneToOneField(basemodels.Contact, parent_link=True,related_name="consumer_contact") service = models.ForeignKey(Service) confidential = models.BooleanField(default=False,help_text='These notes will not be visible to general staff') group = models.ForeignKey(Group,null=True,blank=True) group_contact = models.ForeignKey('consumer.GroupContact',blank=True,null=True) def save(self, *args, **kwargs): super(Contact, self).save(*args,**kwargs) class Meta: permissions = ( ("allow_confidential", "Can make contacts confidential"), ) #ir_contact class Contact(basemodels.Contact): base=models.OneToOneField(BaseContact,parent_link=True,related_name='IR_contact') IR=models.ForeignKey(IR,blank=True,null=True) referred_from=models.ForeignKey(ReferralSource,related_name='IR_referred_from') referred_to=models.ManyToManyField(ReferralSource,related_name='IR_referred_to') information=models.ManyToManyField(Information,verbose_name='Information Requested') }}} the following SQL is produced: {{{ SELECT (fields) FROM `base_contact` LEFT OUTER JOIN `consumer_consumer` ON (`base_contact`.`consumer_id` = `consumer_consumer`.`id`) # ok INNER JOIN `agency_program` ON (`base_contact`.`program_id` = `agency_program`.`id`) # ok LEFT OUTER JOIN `consumer_contact` ON (`base_contact`.`id` = `consumer_contact`.`base_id`) # ok INNER JOIN `base_contact` T5 ON (`consumer_contact`.`base_id` = T5.`id`) # ?!? # These next two are questionable, since they are joined on a LEFT OUTER (so they may potentially not be there) INNER JOIN `consumer_service` ON (`consumer_contact`.`service_id` = `consumer_service`.`id`) INNER JOIN `consumer_servicetype` ON (`consumer_service`.`type_id` = `consumer_servicetype`.`id`) LEFT OUTER JOIN `IR_contact` ON (`base_contact`.`id` = `IR_contact`.`base_id`) # ok INNER JOIN `base_contact` T9 ON (`IR_contact`.`base_id` = T9.`id`) # ?!? WHERE `consumer_consumer`.`file` = 06-1757 ORDER BY `base_contact`.`date` DESC }}} As you can see, there are two INNER JOINs which are messing up this otherwise valid and expected query.
Note:
See TracTickets
for help on using tickets.