﻿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
4510	Reverse relationship ignores to_field	Steven Wagner <stevenwagner@…>	nobody	"The db-api is constructing a query that uses 'id' as the key for the join, when it should be using the to_field='dfp_id' as specified in the model.

{{{
Ad.objects.get(report__date__gte=""2007-06-01"")

SELECT ...
FROM `campaign_line_item_ads` 
INNER JOIN `reports` AS `campaign_line_item_ads__report` 
 ON `campaign_line_item_ads`.`id`                   <-- HERE
     = `campaign_line_item_ads__report`.`ad_id` 
WHERE (`campaign_line_item_ads__report`.`date` >= '2007-06-01')

class Ad(models.Model):
    id = models.IntegerField(primary_key=True)
    campaign_line_item = models.ForeignKey(CampaignLineItem)
    dfp_id = models.IntegerField(null=True, blank=True)
    name = models.CharField(blank=True, maxlength=765)
    class Meta:
        db_table = 'campaign_line_item_ads'
        ordering = ['name']
    def __str__(self):
        return self.name

class Report(models.Model):
    id = models.IntegerField(primary_key=True)
    date = models.DateField(null=True, blank=True)
    ad = models.ForeignKey(Ad, to_field='dfp_id')        <-- Here
    measure = models.IntegerField(null=True, blank=True)
    class Meta: 
        db_table = 'reports'
}}}"		closed	Database layer (models, ORM)	0.96		fixed	to_field, qs-rf-fixed		Accepted	0	0	0	0	0	0
