Changes between Initial Version and Version 1 of Ticket #36430


Ignore:
Timestamp:
Jun 2, 2025, 4:54:46 PM (3 months ago)
Author:
Jacob Walls
Comment:

Here's a rough test after all to demo, just adjust test_max_batch_size like this:

  • tests/bulk_create/tests.py

    diff --git a/tests/bulk_create/tests.py b/tests/bulk_create/tests.py
    index d590a292de..fd30ca48f7 100644
    a b class BulkCreateTests(TestCase):  
    296296    @skipUnlessDBFeature("has_bulk_insert")
    297297    def test_max_batch_size(self):
    298298        objs = [Country(name=f"Country {i}") for i in range(1000)]
    299         fields = ["name", "iso_two_letter", "description"]
     299        fields = ["pk"]
    300300        max_batch_size = connection.ops.bulk_batch_size(fields, objs)
    301301        with self.assertNumQueries(ceil(len(objs) / max_batch_size)):
    302302            Country.objects.bulk_create(objs)

The test fails as it was *more* efficient than expected:

======================================================================
FAIL: test_max_batch_size (bulk_create.tests.BulkCreateTests.test_max_batch_size)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/.../django/tests/bulk_create/tests.py", line 301, in test_max_batch_size
    with self.assertNumQueries(ceil(len(objs) / max_batch_size)):
         ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 1 != 2 : 1 queries executed, 2 expected
Captured queries were:
1. INSERT INTO "bulk_create_country" ("name", "iso_two_letter", "description") VALUES ('Country 0', '', ''), ('Country 1', '', ''), ('Country 2', '', ''), ('Country 3', '', ...

----------------------------------------------------------------------
Ran 2 tests in 0.025s

FAILED (failures=1)

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #36430 – Description

    initial v1  
    55When the list of fields exceeds 1, we go through the `elif` branch and arrive at much higher limits. I'm pretty sure this shows that the limit of 500 for `fields=["pk"]` is overly protective and can just be removed.
    66
    7 I don't have a unit test to provide, but you can play with changing the limit from 500 to 501 and see that `test_large_delete` still passes (no trouble selecting 501 objects).
     7I don't have a unit test to provide, but you can play with changing the limit from 500 to 501 and see that `test_large_delete` still passes (no trouble selecting 501 objects). (You can also adjust `test_max_batch_size()` to provide a s
    88
    99(I found this while trying to [https://github.com/django/django/pull/19502#discussion_r2118612666 refactor] a different call site away from `max_query_params` in the hopes of just calling `bulk_batch_size()` until I saw how overly protective it was.)
Back to Top