Reputation: 135
Getting the error "Table Customer already exists" when i tried to create a table in H2 in memory database using schema.sql. I am using spring boot version: 2.5.4 and below is my pom.xml. It works fine if i use spring boot version: 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>
Here is my code:
@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;
}
}
When i start the app, i am getting the below error. How to force H2 to create the table using schema.sql ?
Caused by: io.r2dbc.spi.R2dbcBadGrammarException: Table "CUSTOMER" already exists; SQL statement: CREATE TABLE CUSTOMER (id SERIAL PRIMARY KEY, name VARCHAR(255)) [42101-200] at io.r2dbc.h2.H2DatabaseExceptionFactory.convert(H2DatabaseExceptionFactory.java:81) ~[r2dbc-h2-0.8.4.RELEASE.jar:0.8.4.RELEASE] at io.r2dbc.h2.H2Statement.lambda$execute$2(H2Statement.java:155) ~[r2dbc-h2-0.8.4.RELEASE.jar:0.8.4.RELEASE] at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:113) ~[reactor-core-3.4.9.jar:3.4.9] ... 44 common frames omitted Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "CUSTOMER" already exists; SQL statement: CREATE TABLE CUSTOMER (id SERIAL PRIMARY KEY, name VARCHAR(255)) [42101-200]
Upvotes: 2
Views: 1854
Reputation: 135
As spring already created the table from the root location schema.sql, ConnectionFactoryInitializer bean is not needed. Removing the ConnectionFactoryInitializer bean declaration fixed the issue.
Upvotes: 4