Opened 15 years ago
Closed 10 years ago
#15844 closed Bug (fixed)
Filtering by related objects causes unnecessary extra db hits when using model inheritance
| Reported by: | Johannes Dollinger | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
The failing test below illustrates the issue.
from django.test import TestCase from django.db import models class A(models.Model): pass class B(A): pass class R(models.Model): b = models.ForeignKey(B, related_name="r") class T(TestCase): def test(self): b = B.objects.create() r = R.objects.create(b=b) # Fails: produces 2 queries self.assertNumQueries(1, lambda: list(R.objects.filter(b=b))) # Fails: produces 2 queries self.assertNumQueries(1, lambda: list(b.r.all())
The extra queries seem to be generated by RelatedField._pk_trace().
Attachments (1)
Change History (7)
comment:1 by , 15 years ago
| Easy pickings: | unset |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
by , 14 years ago
| Attachment: | 15844.r16622.diff added |
|---|
comment:2 by , 14 years ago
| Has patch: | set |
|---|---|
| UI/UX: | unset |
comment:3 by , 14 years ago
| Patch needs improvement: | set |
|---|
comment:4 by , 10 years ago
| Patch needs improvement: | unset |
|---|
Looks like this was fixed in Django 1.6 with 97774429aeb54df4c09895c07cd1b09e70201f7d.
PR to add a regression test.
comment:6 by , 10 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
Why were code comments deleted in patch? They still seem valid, otherwise they should be updated not deleted.