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

标签 h2 spring-data-r2dbc

当我尝试使用 schema.sql 在内存数据库中的 H2 中创建表时,出现错误“表 Customer 已存在”。我使用的是 spring boot 版本:2.5.4,以下是我的 pom.xml。如果我使用 Spring Boot 版本,它工作正常: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创建表?

由以下原因引起:io.r2dbc.spi.R2dbcBadGrammarException:表“CUSTOMER”已存在; SQL 陈述: 创建表客户(id 串行主键,名称 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个常用帧省略 导致:org.h2.jdbc.JdbcSQLSyntaxErrorException:表“CUSTOMER”已存在; SQL 陈述: 创建表客户(id 串行主键,名称 VARCHAR(255))[42101-200]

最佳答案

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

关于h2 - Spring R2DBC - 使用 schema.sql 和 ConnectionFactoryInitializer 创建 h2 表时出现 "Table already exists"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68987990/

相关文章:

spring - 使用H2数据库创建表

java - 如何将H2添加到Wildfly中,以便我可以看到数据库中的数据?

mysql - 在 Spring Boot 下执行 H2

sql - 更新语句根据另一个表的最大行设置列

spring-data-jpa - 是否可以在单个 Spring 引导应用程序中同时使用 Spring Data R2DBC 和 Spring Data JPA?

kotlin - 如何使用reactor和R2dbc压缩嵌套列表

spring-data-r2dbc - 用于多对多映射的 Spring Data r2dbc

h2 - 如何在 Quarkus 应用程序中设置 H2 数据库控制台 url

java - 审核 (@CreatedDate) 不适用于具有反应式 MongoDB 的 WebFlux Spring Boot

spring - @enabler2dbcrepositories 无法找到我的存储库