fix(backend): resolve DetachedInstanceError in audit_log, add pessimistic locks for stock adjustments, and eliminate N+1 queries with eager loading
This commit is contained in:
@ -132,7 +132,8 @@ def create():
|
||||
if not StockModel:
|
||||
return jsonify({'code': 400, 'msg': '无效的库存类型'}), 400
|
||||
|
||||
stock = StockModel.query.get(stock_id)
|
||||
# ★ 修复并发冲突:使用悲观锁锁定行,防止多人同时修改导致的数据覆盖
|
||||
stock = StockModel.query.filter(StockModel.id == stock_id).with_for_update().first()
|
||||
if not stock:
|
||||
return jsonify({'code': 404, 'msg': '库存记录不存在'}), 404
|
||||
|
||||
|
||||
Reference in New Issue
Block a user