Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#7378 closed (fixed)

Reverse relationship ignores to_field (in another setting)

Reported by: mturtle@… Owned by: jacob
Component: Database layer (models, ORM) Version: master
Severity: Keywords: qsrf-cleanup
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

This is related to bug #4510. I am using the latest version from SVN (r7574), and a similar error is happening. Basically, the reverse relationship ignores the to_field when I use it directly as a property (although the example in #4510 using it in a filter works fine).

If I have these models:

class Place(models.Model):
    unique_but_not_primary_field = models.IntegerField(unique=True)

class Franchise(models.Model):
    place = models.ForeignKey(Place, to_field='unique_but_not_primary_field')

And I enter this code:

>>> new_york = Place(unique_but_not_primary_field=1000)
>>> new_york.save()
>>> mcdonalds = Franchise()
>>> mcdonalds.place = new_york
>>> mcdonalds.save()
>>> print new_york.franchise_set.all()
[]
>>> print django.db.connection.queries[-1]['sql']
SELECT `cms_franchise`.`id`, `cms_franchise`.`place_id` FROM `cms_franchise` INNER JOIN `cms_place` ON (`cms_franchise`.`place_id` = `cms_place`.`unique_but_not_primary_field`) WHERE `cms_place`.`id` = 1000 

It is still doing the join on the primary key, not the to_field.

As far as I can tell, the problem is in django/db/models/fields/related.py on line 322:

manager.core_filters = {'%s__pk' % rel_field.name: getattr(instance, rel_field.rel.get_related_field().attname)}

I changed this line to the following, and it works perfectly for me:

manager.core_filters = {'%s__%s' % (rel_field.name, rel_field.rel.get_related_field().attname): getattr(instance, rel_field.rel.get_related_field().attname)}

Attachments (0)

Change History (5)

comment:1 Changed 6 years ago by jacob

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to jacob
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 6 years ago by gav

  • Keywords qsrf-cleanup added

comment:3 Changed 6 years ago by jacob

  • milestone set to 1.0

comment:4 Changed 6 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [7785]) Fixed #7378 -- Use the "to_field" where appropriate on reverse relations.

Patch from mturtle@…. The remaining uses of "%spk" in
fields/related.py all look safe, since they are for many-to-many fields, which
doesn't take "to_field" as a parameter.

comment:5 Changed 3 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.