django 使用mysql数据库在bulk_create上获取事务管理错误

7hiiyaii  于 2022-12-01  发布在  Go
关注(0)|答案(1)|浏览(100)

我尝试使用Django的bulk_create创建一些对象,但是我得到了TransactionManagementError。
Django在django-2.2.24上Mysql是通过docker运行的,我使用的是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.

如果任何人有任何想法可能是根本原因,请给一些提示或东西。任何帮助是感激不尽的。

sg3maiej

sg3maiej1#

也许你应该用transaction.atomic来 Package 你的函数,这里是来自django文档的例子:
从django.db导入事务

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

以及作为上下文管理器:

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()

相关问题