Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7572 -- Force the closure of the database connection at the en…

…d of fixture loading as a workaround for MySQL bug #37735. Thanks to Simon Litchfield for his help in narrowing down this issue.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7803 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9eaa05912c4cbda40a8a8b1085338552507d1599 1 parent 52b6857
Russell Keith-Magee authored June 30, 2008
6  django/core/management/commands/loaddata.py
@@ -162,3 +162,9 @@ def handle(self, *fixture_labels, **options):
162 162
         else:
163 163
             if verbosity > 0:
164 164
                 print "Installed %d object(s) from %d fixture(s)" % (object_count, fixture_count)
  165
+                
  166
+        # Close the DB connection. This is required as a workaround for an
  167
+        # edge case in MySQL: if the same connection is used to
  168
+        # create tables, load data, and query, the query can return
  169
+        # incorrect results. See Django #7572, MySQL #37735.
  170
+        connection.close()
83  tests/regressiontests/fixtures_regress/fixtures/big-fixture.json
... ...
@@ -0,0 +1,83 @@
  1
+[
  2
+    {
  3
+        "pk": 6,
  4
+        "model": "fixtures_regress.channel",
  5
+        "fields": {
  6
+            "name": "Business"
  7
+        }        
  8
+    },
  9
+    
  10
+    {
  11
+        "pk": 1,
  12
+        "model": "fixtures_regress.article",
  13
+        "fields": {
  14
+            "title": "Article Title 1",
  15
+            "channels": [6]
  16
+        }    
  17
+    },
  18
+    {
  19
+        "pk": 2,
  20
+        "model": "fixtures_regress.article",
  21
+        "fields": {
  22
+            "title": "Article Title 2",
  23
+            "channels": [6]
  24
+        }    
  25
+    },
  26
+    {
  27
+        "pk": 3,
  28
+        "model": "fixtures_regress.article",
  29
+        "fields": {
  30
+            "title": "Article Title 3",
  31
+            "channels": [6]
  32
+        }    
  33
+    },
  34
+    {
  35
+        "pk": 4,
  36
+        "model": "fixtures_regress.article",
  37
+        "fields": {
  38
+            "title": "Article Title 4",
  39
+            "channels": [6]
  40
+        }    
  41
+    },
  42
+
  43
+    {
  44
+        "pk": 5,
  45
+        "model": "fixtures_regress.article",
  46
+        "fields": {
  47
+            "title": "Article Title 5",
  48
+            "channels": [6]
  49
+        }    
  50
+    },
  51
+    {
  52
+        "pk": 6,
  53
+        "model": "fixtures_regress.article",
  54
+        "fields": {
  55
+            "title": "Article Title 6",
  56
+            "channels": [6]
  57
+        }    
  58
+    },
  59
+    {
  60
+        "pk": 7,
  61
+        "model": "fixtures_regress.article",
  62
+        "fields": {
  63
+            "title": "Article Title 7",
  64
+            "channels": [6]
  65
+        }    
  66
+    },
  67
+    {
  68
+        "pk": 8,
  69
+        "model": "fixtures_regress.article",
  70
+        "fields": {
  71
+            "title": "Article Title 8",
  72
+            "channels": [6]
  73
+        }    
  74
+    },
  75
+    {
  76
+        "pk": 9,
  77
+        "model": "fixtures_regress.article",
  78
+        "fields": {
  79
+            "title": "Yet Another Article",
  80
+            "channels": [6]
  81
+        }    
  82
+    }
  83
+]
27  tests/regressiontests/fixtures_regress/models.py
@@ -44,6 +44,16 @@ class Parent(models.Model):
44 44
 class Child(Parent):
45 45
     data = models.CharField(max_length=10)
46 46
 
  47
+# Models to regresison check #7572
  48
+class Channel(models.Model):
  49
+    name = models.CharField(max_length=255)
  50
+
  51
+class Article(models.Model):
  52
+    title = models.CharField(max_length=255)
  53
+    channels = models.ManyToManyField(Channel)
  54
+    
  55
+    class Meta:
  56
+        ordering = ('id',)
47 57
 
48 58
 __test__ = {'API_TESTS':"""
49 59
 >>> from django.core import management
@@ -107,4 +117,21 @@ class Child(Parent):
107 117
 
108 118
 >>> management.call_command('loaddata', 'model-inheritance.json', verbosity=0)
109 119
 
  120
+###############################################
  121
+# Test for ticket #7572 -- MySQL has a problem if the same connection is 
  122
+# used to create tables, load data, and then query over that data.
  123
+# To compensate, we close the connection after running loaddata.
  124
+# This ensures that a new connection is opened when test queries are issued.
  125
+
  126
+>>> management.call_command('loaddata', 'big-fixture.json', verbosity=0)
  127
+
  128
+>>> articles = Article.objects.exclude(id=9)
  129
+>>> articles.values_list('id', flat=True)
  130
+[1, 2, 3, 4, 5, 6, 7, 8]
  131
+
  132
+# Just for good measure, run the same query again. Under the influence of
  133
+# ticket #7572, this will give a different result to the previous call.
  134
+>>> articles.values_list('id', flat=True)
  135
+[1, 2, 3, 4, 5, 6, 7, 8]
  136
+
110 137
 """}

0 notes on commit 9eaa059

Please sign in to comment.
Something went wrong with that request. Please try again.