fix(product): 删除引用保护/单价小数位校验,IntegrityError 全局兜底转 400
- delete_product 补 count_refs 引用检查(业务明细+发货明细 RESTRICT 外键)与并发窗口 IntegrityError→400 兜底(customer 先例同构), docstring 与两个 api 层过时注释同步更新 - 产品 schema 复用 schemas/business._check_amount:sale_price 走 (PRICE_MAX, scale=4)、stock_quantity 走 AMOUNT_MAX——此前私写 _parse_non_negative 不查小数位与容量,5 位小数被 Numeric(14,4) 静默截断(舍入即算错钱);新增 6 个秒级 schema 单测锁定口径 - 全局注册 IntegrityError handler:漏补引用保护的删除类操作收敛为 400 通用文案(e2e 撞见一个补一个的模式无法保证全覆盖),完整 异常记日志;rating/customer 的「非 1062 按 500」注释同步更正 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
1 parent
9a70264b51
commit
91fc6f6293
11 files changed
+243
-40
No files matched your search
@@ -6,7 +6,7 @@
|
||||
from sqlalchemy import Select, func, or_, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models import Product
|
||||
from app.models import BusinessItem, Product, ShipmentItem
|
||||
|
||||
# ==================== 读取 ====================
|
||||
|
||||
@@ -100,6 +100,25 @@ def _apply_filters(
|
||||
return stmt
|
||||
|
||||
|
||||
def count_refs(session: Session, product_id: int) -> int:
|
||||
"""统计引用该产品的业务明细与发货明细行数(删除保护,business_id 先例)。
|
||||
|
||||
两张明细表对 products 均 RESTRICT 外键:先查给出可读的 400;校验与
|
||||
删除之间的并发引用由锁行 + FK 兜底(service 层再翻译 IntegrityError)。
|
||||
"""
|
||||
stmt = select(
|
||||
select(func.count())
|
||||
.select_from(BusinessItem)
|
||||
.where(BusinessItem.product_id == product_id)
|
||||
.scalar_subquery()
|
||||
+ select(func.count())
|
||||
.select_from(ShipmentItem)
|
||||
.where(ShipmentItem.product_id == product_id)
|
||||
.scalar_subquery()
|
||||
)
|
||||
return int(session.execute(stmt).scalar_one())
|
||||
|
||||
|
||||
# ==================== 写入 ====================
|
||||
|
||||
|
||||
@@ -111,6 +130,6 @@ def create(session: Session, product: Product) -> Product:
|
||||
|
||||
|
||||
def delete(session: Session, product: Product) -> None:
|
||||
"""物理删除产品。调用方须先完成引用保护校验(V5 就位后生效)。"""
|
||||
"""物理删除产品。调用方须先完成引用保护校验(count_refs + FK 兜底)。"""
|
||||
session.delete(product)
|
||||
session.flush()
|
||||
Reference in new issue
Block a user