admin管理员组文章数量:1122846
Does H2 support mixing between auto generated ID's and INSERTs with explicitly defined ID's? Is there a way to make this work?
Here is a simple example:
DROP TABLE IF EXISTS test;
CREATE TABLE test(
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
title VARCHAR(255)
);
INSERT INTO test(id, title) VALUES (1, 'hello'), (2, 'world');
INSERT INTO test(title) VALUES ('my new value'); -- THIS INSERT FAILS
The last insert fails with the following exception:
Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.TEST(ID) ( /* key:1 */ CAST(1 AS BIGINT), 'hello')"; SQL statement:!
It looks like the auto generation does not detect the existing ID's and tries to use the value 1 again. The documentation does not seem to state anything about this behaviour. It is a pretty common case to have some sql dumps with existing ID's somewhere. After importing these on a new DB, the application will always break. Am I missing something? Please help!
H2 versions tested: 2.2.224, 2.3.232 - both result in errors.
Does H2 support mixing between auto generated ID's and INSERTs with explicitly defined ID's? Is there a way to make this work?
Here is a simple example:
DROP TABLE IF EXISTS test;
CREATE TABLE test(
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
title VARCHAR(255)
);
INSERT INTO test(id, title) VALUES (1, 'hello'), (2, 'world');
INSERT INTO test(title) VALUES ('my new value'); -- THIS INSERT FAILS
The last insert fails with the following exception:
Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.TEST(ID) ( /* key:1 */ CAST(1 AS BIGINT), 'hello')"; SQL statement:!
It looks like the auto generation does not detect the existing ID's and tries to use the value 1 again. The documentation does not seem to state anything about this behaviour. It is a pretty common case to have some sql dumps with existing ID's somewhere. After importing these on a new DB, the application will always break. Am I missing something? Please help!
H2 versions tested: 2.2.224, 2.3.232 - both result in errors.
Share Improve this question asked Nov 22, 2024 at 13:39 maddobmaddob 1,0091 gold badge13 silver badges30 bronze badges 1- 2 Generators of standard identity columns work in that way, see stackoverflow.com/questions/72402946/… for more details. – Evgenij Ryazanov Commented Nov 22, 2024 at 13:52
1 Answer
Reset to default 1My mistake, it is indeed included in the documentation as Evgenij Ryazanov pointed out in the comments. I was able to fix the problem by starting the h2 in mode LEGACY, other modes will work as well. Information about different compatibility modes can be found here
版权声明:本文标题:H2 - GENERATED BY DEFAULT AS IDENTITY conflicting with entries that have manually assigned IDs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303481a1931900.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论