diff --git a/apps/api/src/services/approval_db.py b/apps/api/src/services/approval_db.py index 11cec470..0a635006 100644 --- a/apps/api/src/services/approval_db.py +++ b/apps/api/src/services/approval_db.py @@ -619,16 +619,26 @@ class ApprovalDBService: 以 incident_id 查找最新 PENDING approval record 並回填。 """ async with get_db_context() as db: - values: dict = {"telegram_message_id": telegram_message_id} + # 2026-04-10 Claude Sonnet 4.6: 用 raw SQL 避免 SQLAlchemy 推斷 INTEGER + # telegram_chat_id 為 BIGINT,ORM update() 會誤用 $N::INTEGER 導致 int32 overflow + from sqlalchemy import text as _text + params: dict = { + "incident_id": incident_id, + "telegram_message_id": telegram_message_id, + "status": "PENDING", + } + chat_clause = "" if telegram_chat_id is not None: - values["telegram_chat_id"] = telegram_chat_id + params["telegram_chat_id"] = telegram_chat_id + chat_clause = ", telegram_chat_id = CAST(:telegram_chat_id AS BIGINT)" await db.execute( - update(ApprovalRecord) - .where( - ApprovalRecord.incident_id == incident_id, - ApprovalRecord.status == ApprovalStatus.PENDING, - ) - .values(**values) + _text(f""" + UPDATE approval_records + SET telegram_message_id = :telegram_message_id{chat_clause} + WHERE incident_id = :incident_id + AND status = :status + """), + params, ) # =========================================================================