From 4996eaa7b5b11f822d1c97df79644738df3fed68 Mon Sep 17 00:00:00 2001 From: Ebram Shehata Date: Mon, 13 Jun 2022 06:25:25 +0200 Subject: [PATCH] Made QuerySet.bulk_update() raise an error when batch_size is zero. --- django/db/models/query.py | 2 +- tests/queries/test_bulk_update.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 67fcb411eb..9cfe118e79 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -845,7 +845,7 @@ class QuerySet: """ Update the given fields in each of the given objects in the database. """ - if batch_size is not None and batch_size < 0: + if batch_size is not None and batch_size <= 0: raise ValueError("Batch size must be a positive integer.") if not fields: raise ValueError("Field names must be given to bulk_update().") diff --git a/tests/queries/test_bulk_update.py b/tests/queries/test_bulk_update.py index bc252c21c6..b2688a61c8 100644 --- a/tests/queries/test_bulk_update.py +++ b/tests/queries/test_bulk_update.py @@ -125,6 +125,8 @@ class BulkUpdateTests(TestCase): msg = "Batch size must be a positive integer." with self.assertRaisesMessage(ValueError, msg): Note.objects.bulk_update([], fields=["note"], batch_size=-1) + with self.assertRaisesMessage(ValueError, msg): + Note.objects.bulk_update([], fields=["note"], batch_size=0) def test_nonexistent_field(self): with self.assertRaisesMessage(