Drizzle ORM 连接 TiDB Cloud Serverless 使用 $returningId() 出错求助

如果你插入若干条,实际却返回了极多的 ids, 我觉得可能是产生了溢出导致的。

drizzle-orm 源码遍历 lastInsertId 到 lastInsertId + affectedRows.
但 serverless-js 的 lastInsertId 存在 overflow 的问题:lastInsertId not correct with AUTO_RANDOM id · Issue #65 · tidbcloud/serverless-js · GitHub

你可以检查发生 OOM 时的数据中,id 实际值是否超过 MAX_SAFE_INTEGER,即 9007199254740991.

一个无限循环的例子:

let lastInsertId: number = Number.MAX_SAFE_INTEGER+1;
let rowAffected: number = 5;
let count = 0 
for (let i = lastInsertId; i < lastInsertId + rowAffected; i++) {
  count = count +1 
  if (count > 100) {
    console.log("stop the loop", count);
    break
  }
}

块引用