Fixed #30827 -- Made batch_size arg of QuerySet.bulk_create() respect DatabaseOperations.bulk_batch_size().

Thanks Chetan Khanna for tests.
This commit is contained in:
Ahmet Kucuk 2019-10-01 13:13:19 -04:00 committed by Mariusz Felisiak
parent 3120490912
commit 09578f6dfb
2 changed files with 11 additions and 1 deletions

View File

@ -1209,7 +1209,8 @@ class QuerySet:
if ignore_conflicts and not connections[self.db].features.supports_ignore_conflicts:
raise NotSupportedError('This database backend does not support ignoring conflicts.')
ops = connections[self.db].ops
batch_size = (batch_size or max(ops.bulk_batch_size(fields, objs), 1))
max_batch_size = max(ops.bulk_batch_size(fields, objs), 1)
batch_size = min(batch_size, max_batch_size) if batch_size else max_batch_size
inserted_rows = []
bulk_return = connections[self.db].features.can_return_rows_from_bulk_insert
for item in [objs[i:i + batch_size] for i in range(0, len(objs), batch_size)]:

View File

@ -1,3 +1,4 @@
from math import ceil
from operator import attrgetter
from django.db import IntegrityError, NotSupportedError, connection
@ -214,6 +215,14 @@ class BulkCreateTests(TestCase):
with self.assertNumQueries(1):
TwoFields.objects.bulk_create(objs, len(objs))
@skipUnlessDBFeature('has_bulk_insert')
def test_explicit_batch_size_respects_max_batch_size(self):
objs = [Country() for i in range(1000)]
fields = ['name', 'iso_two_letter', 'description']
max_batch_size = max(connection.ops.bulk_batch_size(fields, objs), 1)
with self.assertNumQueries(ceil(len(objs) / max_batch_size)):
Country.objects.bulk_create(objs, batch_size=max_batch_size + 1)
@skipUnlessDBFeature('has_bulk_insert')
def test_bulk_insert_expressions(self):
Restaurant.objects.bulk_create([