Opened 9 years ago

Last modified 9 years ago

#25685 closed Bug

Deferred model leak — at Version 1

Reported by: Pantelis Petridis Owned by: nobody
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 Pantelis Petridis)

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 (1)

comment:1 by Pantelis Petridis, 9 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top