Spring Boot Error executing DDL drop schema via JDBC Statement

Viewed 1099

Ultimately, my app does do what I want. On startup, it creates the schema with the name that I specified as the default schema, and it creates my tables. On shutdown, it drops the tables and then drops the schema. That's all good.

But it has an error when it starts up, and then it recovers.
On startup, first it does drop table if exists for my tables, and that works fine.
Then it does drop schema "myschema", and that causes this error:

Error executing DDL "drop schema "myschema"" via JDBC Statement
Caused by: org.postgresql.util.PSQLException: ERROR: schema "myschema" does not exist

I really want it to not do that, please. Even though it ultimately recovers.
It was able to do "drop table if exists", so why can't it do "drop schema" the same way?

The only place I specify anything about the schema name is in my application.properties file. Here are some of the properties I have set:

logging.level.org.hibernate.SQL = DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder = TRACE
    
spring.datasource.driver-class-name = org.postgresql.Driver

spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.open-in-view = false
spring.jpa.properties.hibernate.default_schema = "myschema"
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.hbm2dll.create_namespaces = true

spring.jpa.show-sql = true

Versions:
Spring Boot 2.5.3
Java 14
Postgresql jdbc driver 42.2.23
Postgresql database 11.4
Maven 3.8.1 (embedded in Eclipse 2021-06)

2 Answers

You can ignore these errors in general as an error in the drop phase is usually ignored by Hibernate as well. I would suggest you simply don't use create-drop as that only makes sense for "development". I guess you wouldn't want to drop your production tables ;)

Here's how I solved this.

I was already using a custom PostgreSQL dialect in my project. It's defined in a class that extends org.hibernate.dialect.PostgreSQL10Dialect. So I just added a method override for getDropSchemaCommand(String schemaName), and had it return a String starting with "drop schema if exists" instead of "drop schema".

The custom dialect has to be specified in the application.properties file, using spring.jpa.properties.hibernate.dialect.

Related