function-6ren">
gpt4 book ai didi

django - "select_for_update"从原子 block 调用仍然是 TransactionManagementError

转载 作者:行者123 更新时间:2023-11-29 11:35:59 24 4
gpt4 key购买 nike

我在一个大型项目中使用 django。

我从模型的 save 内部调用 celery task,该模型调用一个方法,该方法在循环中调用另一个方法。即:

celery task --> function A()
A() --> for i in range(1,100): call function B()

现在 B() 被一个 atomic() 装饰器包裹,并且在其中有一个 select_for_update 调用。

我仍然得到 TransactionManagementError('select_for_update cannot be used outside a transaction.',)

我不知道这是为什么。我已经通过将任务延迟几秒钟进行了测试,以便在调用任务时提交 save。没有帮助。

我的问题是:当我已经在原子 block 中时,为什么会收到 TransactionManagementError

最佳答案

@ketanbhatt 这可能有帮助

https://docs.djangoproject.com/en/1.9/ref/models/querysets/#select-for-update

Evaluating a queryset with select_for_update() in autocommit mode on backends which support SELECT ... FOR UPDATE is a TransactionManagementError error because the rows are not locked in that case. If allowed, this would facilitate data corruption and could easily be caused by calling code that expects to be run in a transaction outside of one.

https://docs.djangoproject.com/en/1.9/topics/db/transactions/#managing-autocommit

Django will refuse to turn autocommit off when an atomic() block is active, because that would break atomicity.

关于django - "select_for_update"从原子 block 调用仍然是 TransactionManagementError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36299291/

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