Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed SQLite's collapsing of same-valued instances in bulk_create

SQLite used INSERT INTO tbl SELECT %s UNION SELECT %s, the problem
was that there should have been UNION ALL instead of UNION.

Refs #19351
  • Loading branch information...
commit a27582484cf814554907d2d1ad077852de36963f 1 parent 5c81e9d
Anssi Kääriäinen authored November 24, 2012
2  django/db/backends/sqlite3/base.py
@@ -227,7 +227,7 @@ def bulk_insert_sql(self, fields, num_values):
227 227
         res.append("SELECT %s" % ", ".join(
228 228
             "%%s AS %s" % self.quote_name(f.column) for f in fields
229 229
         ))
230  
-        res.extend(["UNION SELECT %s" % ", ".join(["%s"] * len(fields))] * (num_values - 1))
  230
+        res.extend(["UNION ALL SELECT %s" % ", ".join(["%s"] * len(fields))] * (num_values - 1))
231 231
         return " ".join(res)
232 232
 
233 233
 class DatabaseWrapper(BaseDatabaseWrapper):
8  tests/regressiontests/bulk_create/tests.py
@@ -82,6 +82,14 @@ def test_zero_as_autoval(self):
82 82
         with self.assertRaises(ValueError):
83 83
             Country.objects.bulk_create([valid_country, invalid_country])
84 84
 
  85
+    def test_batch_same_vals(self):
  86
+        # Sqlite had a problem where all the same-valued models were
  87
+        # collapsed to one insert.
  88
+        Restaurant.objects.bulk_create([
  89
+            Restaurant(name='foo') for i in range(0, 2)
  90
+        ])
  91
+        self.assertEqual(Restaurant.objects.count(), 2)
  92
+
85 93
     def test_large_batch(self):
86 94
         with override_settings(DEBUG=True):
87 95
             connection.queries = []

0 notes on commit a275824

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