不应该django的模型get_or_create方法包装在一个事务中? [英] Shouldn't django's model get_or_create method be wrapped in a transaction?
问题描述
我正在浏览django的来源,并查看 get_or_create 。
不应该用交易包裹吗?
I was browsing django's source and looked at get_or_create. Shouldn't it be wrapped with a transaction?
谢谢
推荐答案
查看这个diff 看起来像 get_or_create()$中明确处理的trunk / django / db / models / query.py?rev = 8315#L313rel =nofollow>修订版8315 c $ c>方法。
Looking at this diff it looks like as of Revision 8315, that has been handle explicitly within the get_or_create()
method.
正如@reshefm所指出的那样,在 rev 8670 其中 force_insert = True
添加到 obj.save()
确保在竞争条件下,所有实例将尝试插入(而不是回退更新),因此只有一个将成功,而其他将失败。没有实际情况会继续进行另一个 get()
。
As @reshefm pointed out, this was properly solved in rev 8670 where force_insert=True
was added to obj.save()
to ensure that during a race condition, all instances will attempt an insert (and not fall back to update) so only one will succeed while others will fail. Failing instances will proceed to do another get()
.
在发布版本方面,此修复程序是首先介绍在版本1.0中。
In terms of release version, this fix was first introduce in release 1.0.
这篇关于不应该django的模型get_or_create方法包装在一个事务中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!