DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread

I'm having the following issue, when I try to execute my Django tests:

/Users/myUser/Desktop/myProject-py/src/project/project/billing_v2/tests/test_accounting_integration_heading.py::AccountingIntegrationHeadingAPITests::test_create_accounting_integration_heading failed with error: Test failed with exception
request = <SubRequest '_django_setup_unittest' for <TestCaseFunction test_makemigrations_command>>
django_db_blocker = <pytest_django.plugin._DatabaseBlocker object at 0x10602ae80>
    @pytest.fixture(autouse=True, scope="class")
    def _django_setup_unittest(
        request,
        django_db_blocker: "_DatabaseBlocker",
    ) -> Generator[None, None, None]:
        """Setup a django unittest, internal to pytest-django."""
        if not django_settings_is_configured() or not is_django_unittest(request):
            yield
            return
    
        # Fix/patch pytest.
        # Before pytest 5.4: https://github.com/pytest-dev/pytest/issues/5991
        # After pytest 5.4: https://github.com/pytest-dev/pytest-django/issues/824
        from _pytest.unittest import TestCaseFunction
        original_runtest = TestCaseFunction.runtest
    
        def non_debugging_runtest(self) -> None:
            self._testcase(result=self)
    
        try:
            TestCaseFunction.runtest = non_debugging_runtest  # type: ignore[assignment]
    
>           request.getfixturevalue("django_db_setup")
.venv/lib/python3.8/site-packages/pytest_django/plugin.py:490: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.venv/lib/python3.8/site-packages/pytest_django/fixtures.py:122: in django_db_setup
    db_cfg = setup_databases(
.venv/lib/python3.8/site-packages/django/test/utils.py:179: in setup_databases
    connection.creation.create_test_db(
.venv/lib/python3.8/site-packages/django/db/backends/base/creation.py:59: in create_test_db
    self.connection.close()
.venv/lib/python3.8/site-packages/django/utils/asyncio.py:33: in inner
    return func(*args, **kwargs)
.venv/lib/python3.8/site-packages/django/db/backends/base/base.py:285: in close
    self.validate_thread_sharing()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
self = <django.contrib.gis.db.backends.postgis.base.DatabaseWrapper object at 0x118e55ee0>
    def validate_thread_sharing(self):
        """
        Validate that the connection isn't accessed by another thread than the
        one which originally created it, unless the connection was explicitly
        authorized to be shared between threads (via the `inc_thread_sharing()`
        method). Raise an exception if the validation fails.
        """
        if not (self.allow_thread_sharing or self._thread_ident == _thread.get_ident()):
>           raise DatabaseError(
                "DatabaseWrapper objects created in a "
                "thread can only be used in that same thread. The object "
                "with alias '%s' was created in thread id %s and this is "
                "thread id %s."
                % (self.alias, self._thread_ident, _thread.get_ident())
            )
E           django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias 'default' was created in thread id 8482561792 and this is thread id 13188866304.
.venv/lib/python3.8/site-packages/django/db/backends/base/base.py:552: DatabaseError

However, this problem occurs only when, I try to execute all of my tests at once, if I execute them one by one or module by module, it is all good.

Versions Django - 3.2.25 Python - 3.8.20

My DB engine settings in settings.py

"ENGINE": "django.contrib.gis.db.backends.postgis",
Вернуться на верх