[odb-users] odb (2.3.0) with MySQL sql-mode NO_AUTO_VALUE_ON_ZERO

Пустовалов Дмитрий pustovalovdmit at gmail.com
Wed Mar 22 09:53:10 EDT 2017


Hello!

I use MySQl with enabled sql-mode NO_AUTO_VALUE_ON_ZERO, which means that
it's not possible to use 0 in INSERT statements for PK column to indicate
MySQL that it should automatically assign value for PK.

I have persistent object of following definition:

#pragma db objectstruct person{
    #pragma db id auto
    uint64_t id_ = 0;

    std::string first_name_;
    std::string last_name_;};

And table:

CREATE TABLE `person` (`id` BIGINT UNSIGNED NOT NULL
AUTO_INCREMENT,`first_name` VARCHAR(50) NOT NULL,`last_name`
VARCHAR(50) NOT NULL,
 PRIMARY KEY(id)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
collate=utf8_unicode_ci;

I assume that 'id auto' specificator should generate following sql in
mapper classes:

INSERT INTO person (first_name, last_name) VALUES ("John", "Dohn");

or

INSERT INTO person (id, first_name, last_name) VALUES (NULL, "John", "Dohn");

but real sql is:

INSERT INTO person (id, first_name, last_name) VALUES (0, "John", "Dohn");

which leads to inserting row with PK value equal to zero and leads to
duplicate PK errors, instead of inserting row with autoincremented id.

Is there some way to force odb compliter to generate proper SQL-code
because I don't want to disable NO_AUTO_VALUE_ON_ZERO?


More information about the odb-users mailing list