简体   繁体   English

H2 拒绝为 Postgres 模拟数据库创建 auto_increment

[英]H2 refuses to create auto_increment for Postgres emulated database

I created an in memory H2 database with JDBC URL我使用 JDBC URL 创建了一个内存 H2 数据库

jdbc:h2:~/test;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH

The H2 web console refuses to let me do an auto_increment. H2 Web 控制台拒绝让我执行 auto_increment。 I've seen serial for Postgres, but that doesn't work either.我看过 Postgres 的连续剧,但这也不起作用。

At it's simplest, it hates:最简单的是,它讨厌:

create table test(id bigint auto_increment);
Syntax error in SQL statement "create table test(id bigint [*]auto_increment)"; expected "ARRAY, INVISIBLE, VISIBLE, NOT NULL, NULL, AS, DEFAULT, GENERATED, ON UPDATE, NOT NULL, NULL, DEFAULT ON NULL, NULL_TO_DEFAULT, SEQUENCE, SELECTIVITY, COMMENT, CONSTRAINT, COMMENT, PRIMARY KEY, UNIQUE, NOT NULL, NULL, CHECK, REFERENCES, ,, )"; SQL statement:
create table test(id bigint auto_increment) [42001-214] 42001/42001 (Help)

Why do I care:我为什么关心:

My code base was failing with NULL not allowed for column "REV".我的代码库失败,列“REV”不允许使用 NULL。 I'm using JPA/Hibernate + Liquibase.我正在使用 JPA/Hibernate + Liquibase。 In order to try the suggestions at为了尝试在

Hibernate Envers + Liquibase: NULL not allowed for column "REV" Hibernate Envers + Liquibase:列“REV”不允许为 NULL

I'm trying to add an auto_increment to my Liquibase changelog file.我正在尝试将 auto_increment 添加到我的 Liquibase 更改日志文件中。

You can use the SQL Standard's generation clause GENERATED ALWAYS AS IDENTITY .您可以使用 SQL 标准的生成子句GENERATED ALWAYS AS IDENTITY For example:例如:

create table test (
  id bigint generated always as identity, 
  name varchar(10)
);

See PostgreSQL Example .请参阅PostgreSQL 示例

It works the same way in H2.它在 H2 中的工作方式相同。 For example:例如:

create table test(id bigint generated always as identity, name varchar(10));

insert into test (name) values ('Chicago') ;

select * from test;

Result:结果:

ID  NAME   
--  -------
 1  Chicago

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM