﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35238	Missing chunk_size kwarg throws exception when creating test DB if model has base-manager with prefetches	alexcleduc	alexcleduc	"I am trying to upgrade from 4.x to 5.0 and am running into this issue when creating my test database. 

I'm using pytest, but I'd be very surprised if this wasn't also an issue with unit-test. That said, I do find it a little surprising no one has noticed this. Surely there would have been other projects out with models whose base-managers have prefetches? So I'm not ''entirely'' confident this is a bug in django. Maybe pytest should be initializing test DBs differently, I haven't done that research. 


See django/db/backends/base/creation.py for the offending code:

{{{
class BaseDatabaseCreation:
   # ...
    def serialize_db_to_string(self):
        # ... 
        # Iteratively return every object for all models to serialize.
        def get_objects():
            for app_config in apps.get_app_configs():
                if (...):
                    for model in app_config.get_models():
                        if (...):
                            queryset = model._base_manager.using(
                                self.connection.alias,
                            ).order_by(model._meta.pk.name)
                            yield from queryset.iterator() # HERE THROWS EXCEPTION

}}}

Here is the call-stack in my project, you'll see that although it starts with pytest, there's a chain of 6 first-party django calls that lead to the exception:

{{{
  ...
  
  File ""/.../site-packages/pytest_django/fixtures.py"", line 139, in django_db_setup
    db_cfg = setup_databases(
  File "".../site-packages/django/test/utils.py"", line 203, in setup_databases
    connection.creation.create_test_db(
  File "".../site-packages/django/db/backends/base/creation.py"", line 94, in create_test_db
    self.connection._test_serialized_contents = self.serialize_db_to_string()
  File "".../site-packages/django/db/backends/base/creation.py"", line 142, in serialize_db_to_string
    serializers.serialize(""json"", get_objects(), indent=None, stream=out)
  File "".../site-packages/django/core/serializers/__init__.py"", line 134, in serialize
    s.serialize(queryset, **options)
  File "".../site-packages/django/core/serializers/base.py"", line 108, in serialize
    for count, obj in enumerate(queryset, start=1):
  File "".../site-packages/django/db/backends/base/creation.py"", line 138, in get_objects
    yield from queryset.iterator()
}}}

**Proposed solution**
I am putting up a PR that will conditionally use the chunk_size kwarg when calling queryset.iterator() in django/db/backends/base/creation.py. An alternative is to set a fallback chunk_size in iterator() itself, if prefetches are found, but I am guessing there's a good reason that wasn't done in the first place. I am defaulting to the same as aiterator, which is 2000

I am not confident in this solution, there seem to be quite a bit of first-party calls to queryset.iterator that don't include a chunk-size kwarg, however this fix makes my project's relatively large test-suite pass, and I have prefetches on many models' base-managers including the user model. 
"	Bug	closed	Core (Serialization)	5.0	Release blocker	fixed		alexcleduc	Ready for checkin	1	0	0	0	0	0
