CREATE TABLE IF NOT EXISTS `foo` (
`foo_id` INT NOT NULL AUTO_INCREMENT ,
`unique` CHAR(255) NULL ,
`not_unique` CHAR(255) NULL ,
PRIMARY KEY (`foo_id`) ,
UNIQUE INDEX `unique_UNIQUE` (`unique` ASC) )
ENGINE = InnoDB;
This is the table.
INSERT INTO foo (`unique`,`not_unique`) VALUES ('John','Doe')
ON DUPLICATE KEY UPDATE `foo_id`=LAST_INSERT_ID(`foo_id`);
SELECT LAST_INSERT_ID();
LAST_INSERT_ID here returns 1. That is correct.
INSERT INTO foo (`unique`,`not_unique`) VALUES ('John','Doe')
ON DUPLICATE KEY UPDATE `foo_id`=LAST_INSERT_ID(`foo_id`);
SELECT LAST_INSERT_ID();
LAST_INSERT_ID here returns 1. That is correct.
INSERT INTO foo (`unique`,`not_unique`) VALUES ('Jane','Doe')
ON DUPLICATE KEY UPDATE `foo_id`=LAST_INSERT_ID(`foo_id`);
SELECT LAST_INSERT_ID();
LAST_INSERT_ID here returns 3. Why? I was hoping it to be 2. If this is a bug, is there a workaround for it?
The id was taken at the beginning of the attempted insert, and was discarded on failure.