Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Hibernate fails on a CREATE or ALTER TABLE statement at startup

ErrorError executing DDL "alter table events drop foreign key FKg0mkvgsqn8584qoql6a2rxheq" via JDBC Statement

Short answer

Hibernate generated a CREATE, ALTER or DROP statement and MySQL refused it. The useful text is the SQL error under the message. Three causes cover most cases: an entity or column name that is a reserved word, a foreign key whose column types or engine do not match, or ddl-auto running against a schema you manage yourself.

  • Applies to Spring Boot 3.4, Hibernate 6.6, MySQL 8.4, Java 21
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Scroll to the 'Caused by' line under the DDL message: it holds MySQL's own error and the exact statement that failed. Copy that statement and run it by hand in the mysql client.

    grep -n -A6 'Error executing DDL' app.log | head -n 30
    mysql -u app_user -p appdb
  2. 2Match what you see, then jump to its fix

    1. 1The SQL error is a syntax error near a name such as order, group, key or rank in a create table statementA table or column name is a reserved wordFix
    2. 2The failing statement is an alter table ... add constraint ... foreign keyA foreign key does not match its parent column or engineFix
    3. 3Errors on drop or alter statements for tables and constraints that already exist or already differddl-auto is changing a schema that you manage yourselfFix

What does the error look like?

  • Error executing DDL "alter table events drop foreign key FKg0mkvgsqn8584qoql6a2rxheq" via JDBC Statement

Cause 1 of 3

A table or column name is a reserved word

1Check

MySQL only allows a reserved word as an identifier when it is quoted with backticks. ORDER, GROUP, KEY, RANK and ROWS are reserved in MySQL 8.4, while USER is not. A table or column given one of those names needs quoting or a new name.

grep -rn '@Table\|@Entity' src/main/java | head
grep -rn 'ddl-auto' src/main/resources/

2Fix

Rename the table or column in the mapping. If renaming is not an option, ask Hibernate to quote every identifier.

@Entity
@Table(name = "customer_order")
public class Order { }

# or, in application.properties
spring.jpa.properties.hibernate.globally_quoted_identifiers=true

3Verify

Restart the app and check that the create table statement now succeeds.

mysql -u app_user -p appdb -e 'SHOW TABLES;'

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 3

A foreign key does not match its parent column or engine

1Check

MySQL requires parent and child tables to use the same storage engine, columns of similar type with the same size and sign, and an index on the referenced column. An INT parent key and a BIGINT child column, or an old MyISAM table, break it.

SHOW CREATE TABLE parent;
SHOW CREATE TABLE child;

2Fix

Align the column types (change the Java field type or the column definition) and use InnoDB for both tables. Drop a stale foreign key by its real name, which SHOW CREATE TABLE prints.

ALTER TABLE child MODIFY parent_id BIGINT NOT NULL;
ALTER TABLE child DROP FOREIGN KEY child_ibfk_1;

3Verify

Run the app again, or add the constraint by hand and see it succeed.

mysql -u app_user -p appdb -e 'SHOW CREATE TABLE child\G'

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 3

ddl-auto is changing a schema that you manage yourself

1Check

Spring Boot defaults ddl-auto to create-drop only for an embedded database with no Flyway or Liquibase. If you set create, create-drop or update on a real MySQL database, Hibernate tries to drop and alter objects it did not build.

grep -rn 'ddl-auto' src/main/resources/ config/

2Fix

Set ddl-auto to none, or validate to only compare entities with the tables, and manage the schema with scripts or a migration tool.

spring.jpa.hibernate.ddl-auto=validate

3Verify

Restart; the log shows no DDL being executed.

grep -c 'Error executing DDL' app.log

Confirmed by docs.spring.io

Did this fix it?

How do I stop it coming back?

  • Pick table and column names that are not MySQL reserved words.
  • Use none or validate outside local development, and keep the schema in migrations.
  • Create parent and child key columns with identical types.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.