mirror of
https://github.com/MariaDB/server.git
synced 2025-08-31 22:22:30 +03:00
New way to fix BUG#19243 "wrong LAST_INSERT_ID() after ON DUPLICATE KEY UPDATE".
This bug report was two problems: 1) LAST_INSERT_ID() returns a value which does not exist in the table 2) the reporter would want it to return the autoinc id of the updated row. 1) is a real bug, 2) is a feature request. In July I implemented 2) in 5.1 (which automatically fixes 1). This has not yet been documented or released, so is changeable. Precisely, recently Paul and a user found an easy workaround to give 2), which works in 4.1-5.0-5.1. So I can revert my code for 2), because it's not needed, that's what I do here; we forget about 2) (we will document the workaround). But when I revert my code for 2), 1) comes back. We solve 1) by saying that if INSERT ON DUPLICATE KEY UPDATE updates a row, it's like a regular UPDATE: LAST_INSERT_ID() should not be affected (instead of returning a non-existent value). So note: no behaviour change compared to the last released 5.1; just a bugfix for 1). mysql-test/r/innodb_mysql.result: result update mysql-test/t/innodb_mysql.test: test for the new way to fix BUG#19243: that if INSERT ON DUPLICATE KEY UPDATE updates a row, SELECT LAST_INSERT_ID() is not affected. Test of the workaround for people who want SELECT LAST_INSERT_ID() to return the autoinc id of the updated row. sql/sql_insert.cc: No need to change LAST_INSERT_ID() if INSERT ON DUPLICATE KEY UPDATE updates a row, there is a workaround to achieve this without changing code: just add "autoinc_col=LAST_INSERT_ID(autoinc_col)" to your ON DUPLICATE KEY UPDATE clause. Prevent LAST_INSERT_ID() to contain an inexistent value in this case: if the row is updated it should be like a regular UPDATE: don't affect LAST_INSERT_ID() (achieved by marking that we didn't generate an id for this row: insert_id_for_cur_row=0).
This commit is contained in:
@@ -369,8 +369,8 @@ insert into t1 values('aaa');
|
||||
drop table t1;
|
||||
|
||||
# Fix for BUG#19243 "wrong LAST_INSERT_ID() after ON DUPLICATE KEY
|
||||
# UPDATE": now LAST_INSERT_ID() will return the id of the updated
|
||||
# row.
|
||||
# UPDATE": if the row is updated, it's like a regular UPDATE:
|
||||
# LAST_INSERT_ID() is not affected.
|
||||
CREATE TABLE `t2` (
|
||||
`k` int(11) NOT NULL auto_increment,
|
||||
`a` int(11) default NULL,
|
||||
@@ -390,6 +390,12 @@ insert into t2 ( a ) values ( 6 ) on duplicate key update c =
|
||||
ifnull( c,
|
||||
0 ) + 1;
|
||||
select last_insert_id();
|
||||
# test again when last_insert_id() is 0 initially
|
||||
select last_insert_id(0);
|
||||
insert into t2 ( a ) values ( 6 ) on duplicate key update c =
|
||||
ifnull( c,
|
||||
0 ) + 1;
|
||||
select last_insert_id();
|
||||
select * from t2;
|
||||
|
||||
# Test of LAST_INSERT_ID() when autogenerated will fail:
|
||||
@@ -402,5 +408,14 @@ insert ignore into t2 values (null,6,1),(null,8,1),(null,15,1),(null,20,1);
|
||||
select last_insert_id();
|
||||
select * from t2;
|
||||
|
||||
drop table t2;
|
||||
# Test of the workaround which enables people to know the id of the
|
||||
# updated row in INSERT ON DUPLICATE KEY UPDATE, by using
|
||||
# LAST_INSERT_ID(autoinc_col) in the UPDATE clause.
|
||||
|
||||
insert into t2 ( a ) values ( 6 ) on duplicate key update c =
|
||||
ifnull( c,
|
||||
0 ) + 1, k=last_insert_id(k);
|
||||
select last_insert_id();
|
||||
select * from t2;
|
||||
|
||||
drop table t2;
|
||||
|
Reference in New Issue
Block a user