diff --git a/pbl_evidence/api.py b/pbl_evidence/api.py index 201e502..dbe4786 100644 --- a/pbl_evidence/api.py +++ b/pbl_evidence/api.py @@ -25,6 +25,7 @@ import hashlib from pbl_common.api import PblError, actor_id, json_dump, now_str, tenant_id +from pbl_evidence.pk import gen_pk from pbl_evidence.db import ( build_insert, build_where, invalidate_cache, json_loads, pick_columns, q_all, q_exec, q_one, table_columns, @@ -98,6 +99,7 @@ async def pbl_artifact_create(**kw): if st: record['submitted_at'] = st + record['id'] = gen_pk('at') record = pick_columns(record, cols) if 'title' not in record: raise PblError('PBL_E_DB', 'pbl_artifact 缺 title 列,表结构与模型不一致') @@ -273,12 +275,15 @@ async def pbl_evidence_collect(**kw): 'created_at': now_str(), 'updated_at': now_str(), } + record['id'] = gen_pk('ev') record = pick_columns(record, cols) ins_cols = [c for c in record if c in cols] col_sql = ', '.join('`%s`' % c for c in ins_cols) val_sql = ', '.join('${%s}$' % c for c in ins_cols) - dup_cols = [c for c in ins_cols if c not in ('tenant_id', 'source_event_id', - 'evidence_type', 'dedup_key', 'created_at')] + # 'id' 必须排除:主键在插入前由 pk 模块一次性生成(单一落点), + # 命中唯一索引走 ON DUPLICATE 时不能把已存在行的主键改写成本次新生成的值。 + dup_cols = [c for c in ins_cols if c not in ('id', 'tenant_id', 'source_event_id', + 'evidence_type', 'dedup_key', 'created_at')] sql = 'INSERT INTO `pbl_evidence` (%s) VALUES (%s)' % (col_sql, val_sql) if dup_cols: sql += ' ON DUPLICATE KEY UPDATE ' + ', '.join('`%s`=VALUES(`%s`)' % (c, c) for c in dup_cols)