Opened 17 years ago

Closed 17 years ago

#4510 closed (fixed)

Reverse relationship ignores to_field

Reported by: Steven Wagner <stevenwagner@…> Owned by: nobody
Component: Database layer (models, ORM) Version: 0.96
Severity: Keywords: to_field, qs-rf-fixed
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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'

Change History (3)

comment:1 by Malcolm Tredinnick, 17 years ago

Keywords: qs-rf-fixed added

comment:2 by Chris Beaven, 17 years ago

Triage Stage: UnreviewedAccepted

Promoting to accepted: if it's fixed in the queryset refactor then it has obviously an accepted issue.

comment:3 by Malcolm Tredinnick, 17 years ago

Resolution: fixed
Status: newclosed

(In [7477]) Merged the queryset-refactor branch into trunk.

This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.

Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658

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