Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lastInsertId not correct with AUTO_RANDOM id #65

Closed
lucklove opened this issue Jul 7, 2024 · 3 comments
Closed

lastInsertId not correct with AUTO_RANDOM id #65

lucklove opened this issue Jul 7, 2024 · 3 comments

Comments

@lucklove
Copy link

lucklove commented Jul 7, 2024

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
Copy link
Collaborator

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

@shiyuhang0
Copy link
Collaborator

shiyuhang0 commented Jul 8, 2024

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 #66 to prevent the overflow. It will be a break change and we will release v0.2.0 after pr is merged

@shiyuhang0
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants