#25685: Deferred model leak
-------------------------------------+-------------------------------------
     Reporter:  ppetrid              |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.8
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by ppetrid):

 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * needs_docs:   => 0


Old description:

> Consider the following models
>
> {{{
> from django.db import models
>
> class ModelA(models.Model):
>         a = models.CharField(max_length=1)
>
> 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'))
>
>                 # lets'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.

New description:

 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.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/25685#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.2200b46d52069260a7be6b7aff25679c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to