diff options
| author | David Barragán Merino <david.barragan@kaleidos.net> | 2016-10-25 16:51:45 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-10-25 19:22:33 -0400 |
| commit | 736f7e7ed7d04cfb9032636b5b65cba26ec7d6db (patch) | |
| tree | dbcec3a183284b6b12174e222f51e52423bdd57b | |
| parent | 3ca9df26ba4248cc5a3db6837e4f4c4a0b4bf701 (diff) | |
[1.10.x] Fixed #27385 -- Fixed QuerySet.bulk_create() on PostgreSQL when the number of objects is a multiple plus one of batch_size.
Backport of b3bd3aa07c026239dd39d1a37498dfd0a2f09caf from master
| -rw-r--r-- | django/db/models/query.py | 4 | ||||
| -rw-r--r-- | docs/releases/1.10.3.txt | 3 | ||||
| -rw-r--r-- | tests/bulk_create/tests.py | 15 |
3 files changed, 16 insertions, 6 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py index dd2a6001d0..42943a7f28 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1060,9 +1060,9 @@ class QuerySet(object): for item in [objs[i:i + batch_size] for i in range(0, len(objs), batch_size)]: if connections[self.db].features.can_return_ids_from_bulk_insert: inserted_id = self._insert(item, fields=fields, using=self.db, return_id=True) - if len(objs) > 1: + if isinstance(inserted_id, list): inserted_ids.extend(inserted_id) - if len(objs) == 1: + else: inserted_ids.append(inserted_id) else: self._insert(item, fields=fields, using=self.db) diff --git a/docs/releases/1.10.3.txt b/docs/releases/1.10.3.txt index a45124fb3a..5bb340a3a8 100644 --- a/docs/releases/1.10.3.txt +++ b/docs/releases/1.10.3.txt @@ -20,3 +20,6 @@ Bugfixes * Made the ``JavaScriptCatalog`` view respect the ``packages`` argument; previously it was ignored (:ticket:`27374`). + +* Fixed ``QuerySet.bulk_create()`` on PostgreSQL when the number of objects is + a multiple plus one of ``batch_size`` (:ticket:`27385`). diff --git a/tests/bulk_create/tests.py b/tests/bulk_create/tests.py index e6d85cb815..3378f0010a 100644 --- a/tests/bulk_create/tests.py +++ b/tests/bulk_create/tests.py @@ -171,11 +171,18 @@ class BulkCreateTests(TestCase): def test_explicit_batch_size(self): objs = [TwoFields(f1=i, f2=i) for i in range(0, 4)] - TwoFields.objects.bulk_create(objs, 2) - self.assertEqual(TwoFields.objects.count(), len(objs)) + num_objs = len(objs) + TwoFields.objects.bulk_create(objs, batch_size=1) + self.assertEqual(TwoFields.objects.count(), num_objs) TwoFields.objects.all().delete() - TwoFields.objects.bulk_create(objs, len(objs)) - self.assertEqual(TwoFields.objects.count(), len(objs)) + TwoFields.objects.bulk_create(objs, batch_size=2) + self.assertEqual(TwoFields.objects.count(), num_objs) + TwoFields.objects.all().delete() + TwoFields.objects.bulk_create(objs, batch_size=3) + self.assertEqual(TwoFields.objects.count(), num_objs) + TwoFields.objects.all().delete() + TwoFields.objects.bulk_create(objs, batch_size=num_objs) + self.assertEqual(TwoFields.objects.count(), num_objs) def test_empty_model(self): NoFields.objects.bulk_create([NoFields() for i in range(2)]) |
