tidbcloud / serverless-js

A fetch API-compatible driver to connect TiDB Serverless from serverless/edge functions
https://tidb.cloud/
Apache License 2.0
21 stars 4 forks source link

lastInsertId not correct with AUTO_RANDOM id #65

Closed lucklove closed 2 months ago

lucklove commented 3 months ago
CREATE TABLE `t` (
  `id` BIGINT NOT NULL AUTO_RANDOM,
  `name` varchar(128) NOT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
const tx = await conn.begin()

const r = await tx.execute(
    "insert into t (name) values (?)",
    ["banana"],
    { fullResult: true },
)

console.log(r.lastInsertId)

The output will be something like 3746994889972252700 (multiple of 100) but the real id is 3746994889972252673

shiyuhang0 commented 3 months ago

Thanks for your report. Seems there is a precision loss for lastInsertId. I will take a deep look tomorrow.

shiyuhang0 commented 3 months ago

lastInsertId will lose precision when it exceeds the MAX_SAFE_INTEGER of the number type

After some research, we decide to change the type of lastInsertId to string in https://github.com/tidbcloud/serverless-js/pull/66 to prevent the overflow. It will be a break change and we will release v0.2.0 after pr is merged

shiyuhang0 commented 2 months ago

Close as it has been resolved. Please reopen it if have any questions