Getting TransactionManagementError on bulk_create with mysql db

I'm trying to create a few objects using Django's bulk_create but I'm getting TransactionManagementError.

Django is on django-2.2.24 Mysql is running via docker and I'm using mariadb:10.10.2.

Traceback (most recent call last):
  File "/Users/xyz/Documents/dev/django-proj/sliphy/manage.py", line 21, in <module>
    main()
  File "/Users/xyz/Documents/dev/django-proj/sliphy/manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/xyz/opt/miniconda3/envs/.venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/xyz/opt/miniconda3/envs/.venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/xyz/opt/miniconda3/envs/.venv/lib/python3.9/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/xyz/opt/miniconda3/envs/.venv/lib/python3.9/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/Users/xyz/Documents/dev/django-proj/sliphy/generator/management/commands/generate_backup.py", line 31, in handle
    self.bulk_create_into_user_trans()
  File "/Users/xyz/Documents/dev/django-proj/sliphy/generator/management/commands/generate_backup.py", line 78, in bulk_create_trans
    Transactions.objects.bulk_create(objs, batch_size=100)
  File "/Users/xyz/opt/miniconda3/envs/.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 "/Users/xyz/opt/miniconda3/envs/.venv/lib/python3.9/site-packages/cacheops/query.py", line 411, in bulk_create
    objs = self._no_monkey.bulk_create(self, objs, *args, **kwargs)
  File "/Users/xyz/opt/miniconda3/envs/.venv/lib/python3.9/site-packages/django/db/models/query.py", line 474, in bulk_create
    ids = self._batched_insert(objs_without_pk, fields, batch_size, ignore_conflicts=ignore_conflicts)
  File "/Users/xyz/opt/miniconda3/envs/.venv/lib/python3.9/site-packages/django/db/models/query.py", line 1211, in _batched_insert
    self._insert(item, fields=fields, using=self.db, ignore_conflicts=ignore_conflicts)
  File "/Users/xyz/opt/miniconda3/envs/.venv/lib/python3.9/site-packages/django/db/models/query.py", line 1186, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/Users/xyz/opt/miniconda3/envs/.venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1377, in execute_sql
    cursor.execute(sql, params)
  File "/Users/xyz/opt/miniconda3/envs/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/Users/xyz/opt/miniconda3/envs/.venv/lib/python3.9/site-packages/cacheops/transaction.py", line 99, in execute
    result = self._no_monkey.execute(self, sql, params)
  File "/Users/xyz/opt/miniconda3/envs/.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 "/Users/xyz/opt/miniconda3/envs/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/xyz/opt/miniconda3/envs/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 79, in _execute
    self.db.validate_no_broken_transaction()
  File "/Users/xyz/opt/miniconda3/envs/.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.

If anyone has any idea what might be the root cause of this, please do give some hints or something. Any help is appreciated.

Probably you should wrap your function with transaction.atomic, here is example from the django docs:

from django.db import transaction

@transaction.atomic
def viewfunc(request):
    # This code executes inside a transaction.
    do_stuff()

and as a context manager:

from django.db import transaction

def viewfunc(request):
    # This code executes in autocommit mode (Django's default).
    do_stuff()

    with transaction.atomic():
        # This code executes inside a transaction.
        do_more_stuff()
Back to Top