Spring R2DBC - 使用 schema.sql 和 ConnectionFactoryInitializer 创建 h2 table 时出现 "Table already exists" 错误

Spring R2DBC - Getting "Table already exists" error while creating h2 table using schema.sql and ConnectionFactoryInitializer

当我尝试使用 schema.sql 在内存数据库中的 H2 中创建 table 时出现错误“Table 客户已经存在”。我正在使用 spring 引导版本:2.5.4 及以下是我的 pom.xml。如果我使用 spring 引导版本,它工作正常:2.4.3

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.4</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sample</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-r2dbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>io.r2dbc</groupId>
        <artifactId>r2dbc-h2</artifactId>
        </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

这是我的代码:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Customer {
  @Id
  String id;
  String name;
}


//schema.sql
CREATE TABLE CUSTOMER (id SERIAL PRIMARY KEY, name VARCHAR(255));

@SpringBootApplication
public class ReactivedemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(ReactivedemoApplication.class, args);
  }

  @Bean
  ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) {

        ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
        initializer.setConnectionFactory(connectionFactory);
        initializer.setDatabasePopulator(new ResourceDatabasePopulator(new 
        ClassPathResource("schema.sql")));
        return initializer;
   }
}

当我启动应用程序时,出现以下错误。如何强制 H2 使用 schema.sql 创建 table ?

引起:io.r2dbc.spi.R2dbcBadGrammarException:Table“客户”已经存在; SQL 陈述: 创建 TABLE 客户(id SERIAL PRIMARY KEY,名称 VARCHAR(255))[42101-200] 在 io.r2dbc.h2.H2DatabaseExceptionFactory.convert(H2DatabaseExceptionFactory.java:81) ~[r2dbc-h2-0.8.4.RELEASE.jar:0.8.4.RELEASE] 在 io.r2dbc.h2.H2Statement.lambda$execute$2(H2Statement.java:155) ~[r2dbc-h2-0.8.4.RELEASE.jar:0.8.4.RELEASE] 在 reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:113) ~[reactor-core-3.4.9.jar:3.4.9] ...省略了 44 个公共框架 Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "CUSTOMER" 已经存在; SQL 陈述: 创建 TABLE 客户(id SERIAL PRIMARY KEY,名称 VARCHAR(255))[42101-200]

由于 spring 已经从根位置 schema.sql 创建了 table,因此不需要 ConnectionFactoryInitializer bean。删除 ConnectionFactoryInitializer bean 声明解决了这个问题。