Skip to content

Commit

Permalink
[1.4.x] Fixed SQLite's collapsing of same-valued instances in bulk_cr…
Browse files Browse the repository at this point in the history
…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 c7dcb1d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/db/backends/sqlite3/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions tests/regressiontests/bulk_create/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down

0 comments on commit c7dcb1d

Please sign in to comment.