成功创建后未找到Spring H2创建的表

时间:2019-11-12 10:19:56

标签: spring spring-boot h2

我正在使用Spring Boot的schema.sql魔术来创建内存H2数据库。该脚本包含以下语句:

create table PERSON
(
    ID BIGINT not null primary key,
    NAME VARCHAR(255) not null
);

create index IDX_PERSON_NAME on PERSON (NAME);

启动时,Spring Boo失败,但出现以下异常:

Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #2 of URL [file:/D:/git/.../build/resources/main/schema.sql]: create index IDX_PERSON_NAME on PERSON (NAME); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PERSON" not found; SQL statement:
create index IDX_PERSON_NAME on PERSON (NAME) [42102-200]

该语句如何找不到上一条语句中创建的表?

2 个答案:

答案 0 :(得分:0)

这是可能的原因,

  1. 在引用诸如test_schema.person之类的表时需要提及该模式
  2. 您用于创建索引的语法可能是错误的。请参考此链接以获取H2语法https://www.baeldung.com/spring-yaml

答案 1 :(得分:0)

仅因为未提及NAME作为Unique约束,所以您仅将其指定为NOT NULL约束

create table PERSON
(
    ID BIGINT not null primary key,
    NAME VARCHAR(255) not null,
    CONSTRAINT Person_Name_Unique UNIQUE (NAME)

);


create index IDX_PERSON_NAME on PERSON (NAME);