Django 2.2 Upgrade + TransactionManagement Error + During Tests execution

Viewed 170

We are upgrading the django application from version 1.11 to version 2.2 and we have almost done the necessary upgrade, and when we are running the testcases, we are getting error as

django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.

and we are able to run the same testcases in django 1.11.

Even When I run below simple testcase with django 2.2 version is also giving error as mentioned above and its not even printing "hello" statement from setup method.

libraries we are using pytest 4.4, pytest-django-3.4.5 and django 2.2 and djangorestframework 3.11

I have done more research, but could not able to find solution, please advise

from django.test import TestCase
import pytest

@pytest.mark.django_db
class GetValuesTestCase(TestCase):
    def setUp(self):
        print("hello")

    def test_hello(self):

        print("hhh")
        self.assertTrue(True)

Complete Error Stacktrace

> ======================================================== ERRORS ========================================================
> __________________________________ ERROR at setup of GetValuesTestCase.test_hello __________________________________
> 
> request = <SubRequest '_django_setup_unittest' for <TestCaseFunction
> test_hello>> django_db_blocker =
> <pytest_django.plugin._DatabaseBlocker object at 0x7ff3c3007b50>
> 
>     @pytest.fixture(autouse=True, scope="class")
>     def _django_setup_unittest(request, django_db_blocker):
>         """Setup a django unittest, internal to pytest-django."""
>         if django_settings_is_configured() and is_django_unittest(request):
>             request.getfixturevalue("django_test_environment")
> >           request.getfixturevalue("django_db_setup")
> 
> ../usaenv2/lib/python3.7/site-packages/pytest_django/plugin.py:486: 
> _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  ../usaenv2/lib/python3.7/site-packages/pytest_django/fixtures.py:108:
> in django_db_setup
>     **setup_databases_args ../usaenv2/lib/python3.7/site-packages/django/test/utils.py:174: in
> setup_databases
>     serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
> ../usaenv2/lib/python3.7/site-packages/django/db/backends/base/creation.py:72:
> in create_test_db
>     run_syncdb=True, ../usaenv2/lib/python3.7/site-packages/django/core/management/__init__.py:148:
> in call_command
>     return command.execute(*args, **defaults) ../usaenv2/lib/python3.7/site-packages/django/core/management/base.py:364:
> in execute
>     output = self.handle(*args, **options) ../usaenv2/lib/python3.7/site-packages/django/core/management/base.py:83:
> in wrapped
>     res = handle_func(*args, **kwargs) ../usaenv2/lib/python3.7/site-packages/django/core/management/commands/migrate.py:234:
> in handle
>     fake_initial=fake_initial, ../usaenv2/lib/python3.7/site-packages/django/db/migrations/executor.py:117:
> in migrate
>     state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
> ../usaenv2/lib/python3.7/site-packages/django/db/migrations/executor.py:147:
> in _migrate_all_forwards
>     state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
> ../usaenv2/lib/python3.7/site-packages/django/db/migrations/executor.py:246:
> in apply_migration
>     self.record_migration(migration) ../usaenv2/lib/python3.7/site-packages/django/db/migrations/executor.py:261:
> in record_migration
>     self.recorder.record_applied(migration.app_label, migration.name) ../usaenv2/lib/python3.7/site-packages/django/db/migrations/recorder.py:82:
> in record_applied
>     self.ensure_schema() ../usaenv2/lib/python3.7/site-packages/django/db/migrations/recorder.py:62:
> in ensure_schema
>     if self.has_table(): ../usaenv2/lib/python3.7/site-packages/django/db/migrations/recorder.py:56:
> in has_table
>     return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
> ../usaenv2/lib/python3.7/site-packages/django/db/backends/base/introspection.py:48:
> in table_names
>     return get_names(cursor) ../usaenv2/lib/python3.7/site-packages/django/db/backends/base/introspection.py:43:
> in get_names
>     return sorted(ti.name for ti in self.get_table_list(cursor) ../usaenv2/lib/python3.7/site-packages/django/db/backends/postgresql/introspection.py:52:
> in get_table_list
>     """.format('c.relispartition' if self.connection.features.supports_table_partitions else 'FALSE'))
> ../usaenv2/lib/python3.7/site-packages/ddtrace/contrib/dbapi/__init__.py:90:
> in execute
>     return self._trace_method(self.__wrapped__.execute, self._self_datadog_name, query, {}, query, *args, **kwargs)
> ../usaenv2/lib/python3.7/site-packages/ddtrace/contrib/dbapi/__init__.py:44:
> in _trace_method
>     return method(*args, **kwargs) ../usaenv2/lib/python3.7/site-packages/cacheops/transaction.py:99: in
> execute
>     result = self._no_monkey.execute(self, sql, params) ../usaenv2/lib/python3.7/site-packages/django/db/backends/utils.py:67:
> in execute
>     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
> ../usaenv2/lib/python3.7/site-packages/django/db/backends/utils.py:76:
> in _execute_with_wrappers
>     return executor(sql, params, many, context) ../usaenv2/lib/python3.7/site-packages/django/db/backends/utils.py:79:
> in _execute
>     self.db.validate_no_broken_transaction()
> _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
> 
> self = <django.db.backends.postgresql.base.DatabaseWrapper object at
> 0x7ff3c4b140d0>
> 
>     def validate_no_broken_transaction(self):
>         if self.needs_rollback:
>             raise TransactionManagementError(
> >               "An error occurred in the current transaction. You can't "
>                 "execute queries until the end of the 'atomic' block.") E           django.db.transaction.TransactionManagementError:
> An error occurred in the current transaction. You can't execute
> queries until the end of the 'atomic' block.
> 
> ../usaenv2/lib/python3.7/site-packages/django/db/backends/base/base.py:438:
> TransactionManagementError
0 Answers
Related