#18432 closed Bug (fixed)
Chained foreign keys with to_field produce incorrect query
| Reported by: | Max Arnold | Owned by: | Spark23 |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.3 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Example models:
class Model1(models.Model):
pkey = models.IntegerField(unique=True, db_index=True)
class Model2(models.Model):
model1 = models.ForeignKey(Model1, unique=True, to_field='pkey')
class Model3(models.Model):
model2 = models.ForeignKey(Model2, unique=True, to_field='model1')
Steps to reproduce:
m1 = Model1(pkey=1000); m1.save(); m2 = Model2(model1=m1); m2.save(); m3 = Model3(model2=m2); m3.save() (0.000) INSERT INTO `testcase_model1` (`pkey`) VALUES (1000); args=(1000,) (0.000) INSERT INTO `testcase_model2` (`model1_id`) VALUES (1000); args=(1000,) (0.000) INSERT INTO `testcase_model3` (`model2_id`) VALUES (1000); args=(1000,) m3 = Model3.objects.get(model2=1000) (0.001) SELECT `testcase_model3`.`id`, `testcase_model3`.`model2_id` FROM `testcase_model3` WHERE `testcase_model3`.`model2_id` = 1000 ; args=(1000,) m3.model2 (0.000) SELECT `testcase_model2`.`id`, `testcase_model2`.`model1_id` FROM `testcase_model2` INNER JOIN `testcase_model1` ON (`testcase_model2`.`model1_id` = `testcase_model1`.`pkey`) WHERE `testcase_model1`.`id` = 1000 ; args=(1000,)
In the last query WHERE condition is incorrect - it does not respect to_field attribute and uses primary key instead.
Error message:
DoesNotExist Traceback (most recent call last)
/private/tmp/fkchain/<ipython-input-5-2fce98c5af10> in <module>()
----> 1 m3.model2
/Library/Python/2.7/site-packages/django/db/models/fields/related.pyc in __get__(self, instance, instance_type)
313 rel_obj = rel_mgr.using(db).get(**params)
314 else:
--> 315 rel_obj = QuerySet(self.field.rel.to).using(db).get(**params)
316 setattr(instance, cache_name, rel_obj)
317 return rel_obj
/Library/Python/2.7/site-packages/django/db/models/query.pyc in get(self, *args, **kwargs)
347 if not num:
348 raise self.model.DoesNotExist("%s matching query does not exist."
--> 349 % self.model._meta.object_name)
350 raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s"
351 % (self.model._meta.object_name, num, kwargs))
DoesNotExist: Model2 matching query does not exist.
Change History (5)
comment:1 by , 13 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:2 by , 13 years ago
| Has patch: | set |
|---|---|
| Resolution: | → fixed |
| Status: | assigned → closed |
| Triage Stage: | Unreviewed → Ready for checkin |
comment:3 by , 13 years ago
| Resolution: | fixed |
|---|---|
| Status: | closed → reopened |
comment:4 by , 13 years ago
| Resolution: | → fixed |
|---|---|
| Status: | reopened → closed |
Note:
See TracTickets
for help on using tickets.
fixed in pull request #126 on github