Permalink
Browse files

[1.4.x] Fixed SQLite's collapsing of same-valued instances in bulk_cr…

…eate

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

Backpatch of a275824
  • Loading branch information...
akaariai committed Nov 23, 2012
1 parent 9ee9a72 commit c7dcb1d808597e0806fb352bb5ddc4b58c452f4c
Showing with 9 additions and 1 deletion.
  1. +1 −1 django/db/backends/sqlite3/base.py
  2. +8 −0 tests/regressiontests/bulk_create/tests.py
@@ -215,7 +215,7 @@ def bulk_insert_sql(self, fields, num_values):
res.append("SELECT %s" % ", ".join(
"%%s AS %s" % self.quote_name(f.column) for f in fields
))
- res.extend(["UNION SELECT %s" % ", ".join(["%s"] * len(fields))] * (num_values - 1))
+ res.extend(["UNION ALL SELECT %s" % ", ".join(["%s"] * len(fields))] * (num_values - 1))
return " ".join(res)
class DatabaseWrapper(BaseDatabaseWrapper):
@@ -59,6 +59,14 @@ def test_non_auto_increment_pk(self):
"CA", "IL", "ME", "NY",
], attrgetter("two_letter_code"))
+ def test_batch_same_vals(self):
+ # Sqlite had a problem where all the same-valued models were
+ # collapsed to one insert.
+ Restaurant.objects.bulk_create([
+ Restaurant(name='foo') for i in range(0, 2)
+ ])
+ self.assertEqual(Restaurant.objects.count(), 2)
+
def test_large_batch(self):
with override_settings(DEBUG=True):
connection.queries = []

0 comments on commit c7dcb1d

Please sign in to comment.