mirror of
https://github.com/django/django.git
synced 2025-05-11 09:26:29 +00:00
[1.4.x] 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 Backpatch of a27582484cf814554907d2d1ad077852de36963f
This commit is contained in:
parent
9ee9a7265a
commit
c7dcb1d808
@ -215,7 +215,7 @@ class DatabaseOperations(BaseDatabaseOperations):
|
|||||||
res.append("SELECT %s" % ", ".join(
|
res.append("SELECT %s" % ", ".join(
|
||||||
"%%s AS %s" % self.quote_name(f.column) for f in fields
|
"%%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)
|
return " ".join(res)
|
||||||
|
|
||||||
class DatabaseWrapper(BaseDatabaseWrapper):
|
class DatabaseWrapper(BaseDatabaseWrapper):
|
||||||
|
@ -59,6 +59,14 @@ class BulkCreateTests(TestCase):
|
|||||||
"CA", "IL", "ME", "NY",
|
"CA", "IL", "ME", "NY",
|
||||||
], attrgetter("two_letter_code"))
|
], 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):
|
def test_large_batch(self):
|
||||||
with override_settings(DEBUG=True):
|
with override_settings(DEBUG=True):
|
||||||
connection.queries = []
|
connection.queries = []
|
||||||
|
Loading…
x
Reference in New Issue
Block a user