Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@akaariai akaariai authored
View
2  django/db/backends/sqlite3/base.py
@@ -227,7 +227,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):
View
8 tests/regressiontests/bulk_create/tests.py
@@ -82,6 +82,14 @@ def test_zero_as_autoval(self):
with self.assertRaises(ValueError):
Country.objects.bulk_create([valid_country, invalid_country])
+ 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 a275824

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