首页 > 解决方案 > 创建成功后找不到Spring 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]

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

标签: springspring-booth2

解决方案


以下是可能的原因,

  1. 在引用表时需要提及架构test_schema.person
  2. 您创建索引的语法可能是错误的。有关 H2 语法,请参阅此链接,https://www.baeldung.com/spring-yaml

推荐阅读