0

I have a django project with hundreds of unit tests and after a big update that added about 50 more tests, all the project's tests that creates an instance of a specific model are failing with the following error message:

Traceback (most recent call last):
  File ".../venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1142, in execute_sql
    cursor.execute(sql, params)
  File ".../venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File ".../venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ".../venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 79, in _execute
    self.db.validate_no_broken_transaction()
  File ".../venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 437, in validate_no_broken_transaction
    raise TransactionManagementError(
django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.

During handling of the above exception, another exception occurred:
  File ".../venv/lib/python3.9/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File ".../venv/lib/python3.9/site-packages/django/db/models/query.py", line 653, in first
    for obj in (self if self.ordered else self.order_by('pk'))[:1]:
  File ".../venv/lib/python3.9/site-packages/django/db/models/query.py", line 274, in __iter__
    self._fetch_all()
  File ".../venv/lib/python3.9/site-packages/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File ".../venv/lib/python3.9/site-packages/django/db/models/query.py", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File ".../venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1145, in execute_sql
    cursor.close()
  File ".../venv/lib/python3.9/site-packages/MySQLdb/cursors.py", line 83, in close
    while self.nextset():
  File ".../venv/lib/python3.9/site-packages/MySQLdb/cursors.py", line 137, in nextset
    nr = db.next_result()
MySQLdb._exceptions.OperationalError: (2006, '')

The update did not change any models at all and no migrations were made.

The tests seem to work if I change class SomeTest(TestCase) to class SomeTest(TransactionTestCase) in all tests, but then they get extremely slow and take several hours to run. (They usually took 2 minutes two run before this error.)

I'm using python 3.9, django 2.2.28, and mysql 8.0.23.

I tried upgrading to django 3.2 but that did not fix the problem.

I also tried not running all the new tests with --exclude-tag on the command line, but it would still raise the same errors.

  • [this question](https://stackoverflow.com/questions/21458387/transactionmanagementerror-you-cant-execute-queries-until-the-end-of-the-atom) has a few answers that might be applicable to your situation. All of the answers are about testing. Seems like your database was left in a weird state after a unit test though. – castis Jul 26 '22 at 18:01
  • @castis I already tried the solutions of these answers but none of them worked for me. – Romero Cartaxo Jul 26 '22 at 18:09
  • I also tried not to run the new tests created but the old tests still raise the same errors – Romero Cartaxo Jul 27 '22 at 16:56

0 Answers0