0

Using MySQL mode in H2, found a situation that works for MySQL but not H2.

If inserting and specifying NULL value for an AUTO_INCREMENT column, if there is a duplicate key conflict, H2 silently fails to apply the ON DUPLICATE KEY UPDATE clause.

A simple example:

DROP TABLE IF EXISTS `example`;
CREATE TABLE `example` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `hostname` varchar(64) NOT NULL,
  `exit_code` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `host` (`hostname`),
);

INSERT INTO example (id, hostname, exit_code)
  VALUES (NULL, 'host', 0) 
  ON DUPLICATE KEY UPDATE exit_code = 0;
SELECT * FROM example;

INSERT INTO example (id, hostname, exit_code)
  VALUES (NULL, 'host', 14) 
  ON DUPLICATE KEY  UPDATE exit_code = 14;
SELECT * FROM example;

The result of that last select shows exit_code 0, when it should be 14.

There is a simple workaround, to change the last INSERT to:

INSERT INTO example (hostname, exit_code)
  VALUES ('host', 14) 
  ON DUPLICATE KEY UPDATE exit_code = 14;

Then it works. Wanted to post here with the question: Do you agree this is a bug?

  • What version of MySQL? It works for me on 5.6.12 -- both InnoDB and MyISAM. (Otherwise, I would agree with you.) I glanced at the changelogs and did not spot any relevant changes. – Rick James Mar 07 '15 at 06:24
  • Not A MySQL bug, sorry for the confusion. It's a bug in the MySQL compatibility mode of H2 database. – Jack Humphrey Mar 09 '15 at 00:00

0 Answers0