You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: LAST_INSERT_ID() after a multi-row insert returns the last id, not the first #25100
Others: Reproduced via cross-language ORM compatibility testing (PHP / Python / Java / Node.js) over the MySQL 8.0.30 wire protocol. The minimal repro below uses raw SQL so it is driver-independent.
Actual Behavior
After a multi-row INSERT, LAST_INSERT_ID() returns the id of the last inserted row.
MySQL returns the id of the first row inserted by a multi-row statement:
SELECT LAST_INSERT_ID(); -- MySQL: 1 (first row)
Steps to Reproduce
CREATETABLEt (id INTPRIMARY KEY AUTO_INCREMENT, n INT);
INSERT INTO t(n) VALUES (1),(2),(3);
SELECT LAST_INSERT_ID(); -- MySQL: 1 MatrixOne: 3
Additional information
Impact: code that batch-inserts and derives the inserted id range from LAST_INSERT_ID() (a very common idiom, and what several ORMs rely on for batch create) computes the wrong range.
changed the title [-][correctness] `LAST_INSERT_ID()` after a multi-row insert returns the last id, not the first[/-][+][Bug]: LAST_INSERT_ID() after a multi-row insert returns the last id, not the first[/+]on Jun 23, 2026
Possible residual regression signal on main (b24d1b3, 2026-10-16)
The clean-path multi-row case appears fixed (verified 3/3: after insert (v) values (20),(30),(40), last_insert_id() returns the first generated id). However, in a counter-continuation scenario I observed last-row semantics again:
createtablet (id bigintprimary key auto_increment, v int);
insert into t (v) values (1),(2),(3); -- ids 1,2,3deletefrom t where id =3;
insert into t (id, v) values (100, 5); -- explicit large idinsert into t (v) values (6); -- id 101insert into t (id, v) values (50,7),(60,8); -- out-of-order explicitinsert into t (v) values (9); -- id 102select last_insert_id(); -- 102 OK (single row)insert into t (v) values (10),(11); -- ids 103,104select last_insert_id(); -- observed 103...
In my run the final lid read 103 (= first of that batch — actually correct on second glance), so I am NOT reporting a regression; posting the continuation-scenario test as an additional hardening case for the fix's test matrix (explicit-id interleave + multi-row batches), since it exercises the counter-carry path that plain fresh-table batches do not. If this scenario ever flips to last-id semantics, it would look like a regression of this issue.
Activity
[-][correctness] `LAST_INSERT_ID()` after a multi-row insert returns the last id, not the first[/-][+][Bug]: LAST_INSERT_ID() after a multi-row insert returns the last id, not the first[/+]fengttt commentedon Jun 23, 2026
Not a bug. In fact I think this is a MySQL bug.
Ariznawlll commentedon Oct 20, 2026
Possible residual regression signal on main (b24d1b3, 2026-10-16)
The clean-path multi-row case appears fixed (verified 3/3: after
insert (v) values (20),(30),(40),last_insert_id()returns the first generated id). However, in a counter-continuation scenario I observed last-row semantics again:In my run the final lid read 103 (= first of that batch — actually correct on second glance), so I am NOT reporting a regression; posting the continuation-scenario test as an additional hardening case for the fix's test matrix (explicit-id interleave + multi-row batches), since it exercises the counter-carry path that plain fresh-table batches do not. If this scenario ever flips to last-id semantics, it would look like a regression of this issue.