【问题标题】:Spring H2 created table not found after successful creation创建成功后找不到Spring H2创建的表
【发布时间】:2019-11-12 10:19:56
【问题描述】:

我正在使用 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]

语句怎么找不到前面语句创建的表?

【问题讨论】:

    标签: spring spring-boot h2


    【解决方案1】:

    仅仅因为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);
    

    【讨论】:

      【解决方案2】:

      以下是可能的原因,

      1. 在引用 test_schema.person 之类的表时需要提及架构
      2. 您创建索引的语法可能有误。 H2语法参考这个链接,https://www.baeldung.com/spring-yaml

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-03
        • 2019-06-12
        • 2021-08-12
        • 1970-01-01
        • 2019-10-23
        • 2020-12-14
        • 2020-10-21
        • 2019-07-07
        相关资源
        最近更新 更多