gpt4 book ai didi

python - Django 保存点回滚捕获完整性错误导致 TransactionManagementError

转载 作者:行者123 更新时间:2023-11-29 13:21:36 36 4
gpt4 key购买 nike

我在 Django 的 transaction.atomic block 中运行以下代码。

@transaction.atomic()
def test():
a.save()
sid = transaction.savepoint()
try:
b.save()
transaction.savepoint_commit(sid)
except IntegrityError as e:
transaction.savepoint_rollback(sid)
c.save()

此代码给我以下错误 -

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

我点击了官方文档中的以下链接。 https://docs.djangoproject.com/en/1.10/topics/db/transactions/#s-savepoint-rollback

我在这里错过了什么?

编辑:-添加堆栈跟踪。

    File "/Users/vibhor/Documents/juggernaut/user-venv-new/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/vibhor/Documents/juggernaut/user-venv-new/lib/python2.7/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/Users/vibhor/Documents/juggernaut/user-persistence-service/books/models/books.py", line 243, in save
transaction.savepoint_rollback(sid)
File "/Users/vibhor/Documents/juggernaut/user-venv-new/lib/python2.7/site-packages/django/db/transaction.py", line 66, in savepoint_rollback
get_connection(using).savepoint_rollback(sid)
File "/Users/vibhor/Documents/juggernaut/user-venv-new/lib/python2.7/site-packages/django/db/backends/base/base.py", line 328, in savepoint_rollback
self._savepoint_rollback(sid)
File "/Users/vibhor/Documents/juggernaut/user-venv-new/lib/python2.7/site-packages/django/db/backends/base/base.py", line 288, in _savepoint_rollback
cursor.execute(self.ops.savepoint_rollback_sql(sid))
File "/Users/vibhor/Documents/juggernaut/user-venv-new/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/vibhor/Documents/juggernaut/user-venv-new/lib/python2.7/site-packages/django/db/backends/utils.py", line 59, in execute
self.db.validate_no_broken_transaction()
File "/Users/vibhor/Documents/juggernaut/user-venv-new/lib/python2.7/site-packages/django/db/backends/base/base.py", line 429, in validate_no_broken_transaction
"An error occurred in the current transaction. You can't "
TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.

最佳答案

我认为您遇到了文档中描述的这个问题:

Savepoints may be used to recover from a database error by performing a partial rollback. If you’re doing this inside an atomic() block, the entire block will still be rolled back, because it doesn’t know you’ve handled the situation at a lower level! To prevent this, you can control the rollback behavior with the following functions...

如此处所述,您可能想要执行 transaction.set_rollback(False) 以防止回滚整个原子 block 。

现在,您手动执行此操作是否有原因?您发布的代码可以使用嵌套原子 block 完成相同的事情,并且如文档所述:

When the atomic() decorator is nested, it creates a savepoint to allow partial commit or rollback. You’re strongly encouraged to use atomic() rather than the functions described below.

关于python - Django 保存点回滚捕获完整性错误导致 TransactionManagementError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40671968/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com