#25685 closed Bug (fixed)
Model.delete() issues extra queries after a deferred queryset
Reported by: | Pantelis Petridis | Owned by: | Simon Charette |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.8 |
Severity: | Release blocker | 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 (last modified by )
Consider the following models
from django.db import models class ModelA(models.Model): a = models.CharField(max_length=15) class ModelB(models.Model): fk = models.ForeignKey(ModelA)
and the following test
from django.test import TestCase from .models import ModelA class DeferProblemTestCase(TestCase): def test_defer(self): a = ModelA.objects.create(a='test') with self.assertNumQueries(2): a.delete() unrelated_qs = list(ModelA.objects.defer('a')) # let's try once again a = ModelA.objects.create(a='test') with self.assertNumQueries(2): a.delete()
The above test fails under django 1.8.6, as the second call to .delete()
will produce 3 queries instead of 2. It seems django somehow caches the deferred model and tries to delete any related ModelB records twice. Except from the overhead, this can lead to random test fails based on the execution order.
Change History (12)
comment:1 by , 9 years ago
Description: | modified (diff) |
---|
comment:2 by , 9 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
Status: | new → assigned |
Triage Stage: | Unreviewed → Accepted |
Version: | 1.8 → master |
comment:3 by , 9 years ago
Is there any chance this gets backported to django 1.8, since #18012 is a Cleanup/optimization but this one's a bug?
comment:4 by , 9 years ago
I don't think back porting should apply here since this isn't a bug in a new feature.
comment:5 by , 9 years ago
I came up across this issue while upgrading from django 1.7 to 1.8. I can confirm the above test works in 1.7.XX and the issue was introduced in 1.8. But since I'm not familiar with the release cycle I don't know if that would justify a back port. Thanks anyway!
comment:6 by , 9 years ago
Severity: | Normal → Release blocker |
---|
If it's a regression in 1.8 then I think a fix should be backported.
I think we should avoid backporting the #18012 related patches and provide a more localized fix.
comment:7 by , 9 years ago
Has patch: | unset |
---|
As far as I can tell, there isn't a patch to review yet.
comment:9 by , 9 years ago
Summary: | Deferred model leak → Model.delete() issues extra queries after a deferred queryset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
This has been in the process of solving #18012 (8bdfabed6563f2ae136ad43e05bb254c9c15811a).
I guess adding this regression test case to
tests/delete
wouldn't hurt at this point.