The App Started Without Its Category Column: Moving from ddl-auto=update to Flyway
After the "hello" page in Part 2, I added post storage. I started with spring.jpa.hibernate.ddl-auto=update, letting Hibernate create and alter tables to match the entities.
Adding categories broke that setup. The app started without a required column, and publishing a post returned 500.
Making a category required on every post
The first feature step created a posts table with little more than a title, body, slug, and publish time. In the second step, I added categories to give the blog its shape. Every post had to belong to exactly one category, so I put a required foreign key on Post.
@ManyToOne(optional = false)@JoinColumn(name = "category_id", nullable = false)private Category category;I also added a summary column, excerpt, which could be left empty.
It booted, but publishing returned 500
I started the new version while keeping the database volume that held the posts table from the first step. The log showed this error:
ERROR: column "category_id" referenced in foreign key constraint does not exist Error executing DDL "alter table if exists posts add constraint ... foreign key (category_id) ..."The odd part was that only some of the changes were applied. The nullable excerpt column was quietly added, but the required category_id foreign key was not. The application was up, yet publishing a post returned a 500 error.
Who decides the values for existing rows?
I could confirm three things: excerpt had been added, category_id was missing, and the foreign-key DDL had failed. The diagram shows those observations.
Preparing diagram.
Diagram source
flowchart TB
U[Automatic schema update] --> A[excerpt added]
U --> B[category_id missing]
B --> F[Foreign-key DDL fails]
A --> P[App starts · publishing returns 500]
F --> PThe notes do not preserve the row count or the first error from adding the column. I therefore cannot say that existing rows caused this failure. The "zero posts" checked several days later, just before adopting Flyway, does not establish the row count at the time of the incident either.
Still, adding a required column means deciding what to do with existing rows. In PostgreSQL, adding a column without a default leaves existing rows with null. If the table has rows, immediately requiring NOT NULL cannot work. [S2] A decision such as "assign every existing post to Tech" needs a step that fills those values. The entity's nullable = false does not express that decision.
What this incident did establish was that the app could start despite a schema update error. A successful boot did not mean the schema change was complete.
The fix at the time: throw away the volume
This was still development, so the database had no data worth keeping. As the plan had anticipated, I discarded the volume entirely and started over. This is the command I ran then; it also deletes the data in the volume.
docker compose -f infra/docker-compose.yml down -vHibernate created a fresh posts table that included category_id, and the category seed started clean. I learned one extra thing while hunting for the volume: because the compose project was named infra, the volume was called infra_pgdata. Searching for the blog's name (forge) wouldn't find it.
Once there were posts to preserve, I would need a different approach.
Why I moved to Flyway right then
A few days later, on July 1, I moved schema management to Flyway. The reason was timing. The next features (cover images and draft status) all required schema changes, and there were still zero production posts. I could make the switch before there was post data to migrate.
Flyway applies numbered SQL files such as V1__init.sql and V2__...sql in order, once each, and records how far it has gotten in the flyway_schema_history table. The history of schema changes lives in Git as SQL, and how to migrate existing data is written into that SQL by a person.
The Spring Boot docs also recommend using a single mechanism for schema generation. [S1] So when I adopted Flyway, I changed Hibernate's role from creating the schema to checking it.
Start over instead of using a baseline
One way to introduce Flyway to an existing database is to record a baseline. Migrations up to that version are skipped; later versions are applied. [S3] Recording a baseline does not validate the existing schema or back up its data.
This blog had nothing to preserve. The seeder recreated the categories, and there were zero posts. So I skipped the baseline, threw the volume away once more, and started from V1. V1 pinned down, in SQL, the schema Hibernate had been generating until then.
create table posts ( id bigint generated by default as identity, slug varchar(255) not null, ... category_id bigint not null, ...);alter table if exists posts add constraint uk_posts_slug unique (slug);alter table if exists posts add constraint fk_posts_category foreign key (category_id) references categories (id);Use validate in production, and separately check what validate ignores
I switched the production setting to ddl-auto=validate. Flyway builds the schema, and Hibernate checks things such as mapped tables, columns, and types. A mismatch it detects stops the app from booting. That does not mean it catches every schema difference.
spring.jpa.hibernate.ddl-auto=validateThe Hibernate 7.2 validation implementation checks mapped sequences as well as tables, columns, and types. [S4] The details vary by version and configuration, so enabling validate alone does not establish that every NOT NULL, UNIQUE, and foreign-key constraint is correct. Leaving a foreign key out of V1 can therefore go undetected by validate.
So I verified the constraints in two other ways:
- When writing V1, I matched the constraints to the DDL Hibernate had actually been generating.
- On the production Mac, I tested the constraints directly. I tried inserting a post with a category ID that didn't exist to confirm the foreign key really rejected it, then rolled back the transaction so nothing was left behind.
In the process I confirmed that the three constraints uk_categories_slug, uk_posts_slug, and fk_posts_category actually existed. After a restart, I also saw No migration necessary and the history table still holding a single row.
The boot order works out like this:
- Flyway applies any V files it hasn't applied yet.
- Hibernate checks mapped tables, columns, types, and other supported mappings.
- The application context is ready.
- The category seeder runs.
Because Flyway creates the schema first, the categories table is guaranteed to exist by the time the seeder touches it.
Turn Flyway off in tests
At the time, the default tests used an in-memory H2 database with Hibernate's create-drop to build and remove the schema. I disabled Flyway in the test configuration to keep that setup when introducing it.
spring.flyway.enabled=falseThose tests used a fresh schema created from the entities. Passing them did not show whether a migration would work on PostgreSQL with existing data. For V7, described below, I checked that with a temporary PostgreSQL container.
Flyway quietly didn't run
After adding the Flyway library and starting it on the production Mac, boot kept failing with missing table [categories]. There was not a single Flyway line in the logs. It hadn't failed. It had never run at all.
The Spring Boot 4.1 setup I was using also needed the Flyway auto-configuration module, and that dependency was missing. The module split was introduced in 4.0, not 4.1. [S5] I'll cover that in detail in the next post, which is about "passing tests ≠ a booting app."
Filling existing rows in V5 and V7
For later required columns, I wrote the values for existing rows into the SQL as well. V5 and V7 show two ways I did that.
V5: add, fill, then require
Supporting both Korean and English required a mandatory English name (name_en) on categories. This time, the production database already had two seeded category rows. This time, the need to choose values for existing rows was clear.
With Flyway, how to fill existing rows can be written directly in SQL. I added the column as nullable, filled in the values, and then made it required.
alter table categories add column name_en varchar(255);update categories set name_en = 'Tech' ... where slug = 'tech';update categories set name_en = name where name_en is null; -- guard against unexpected rowsalter table categories alter column name_en set not null;In the same file, though, the English title and body on posts went straight in as NOT NULL. That worked only because there were zero production posts, and it was safe only because a step right before deployment confirmed the post count really was zero.
V7: reproducing production on a throwaway database first
The feature for choosing which languages each post is published in added a NOT NULL DEFAULT 'KO,EN' column. With a default value, existing posts get filled in automatically.
I also added a test that ran the V7 SQL directly on H2 to check that it filled existing rows. But running one SQL file on H2 did not verify the Flyway migration on PostgreSQL. So without touching the production database, I started a throwaway PostgreSQL container with a different name and port, and reproduced it step by step:
- Applied V1 through V7 and booted with validate to confirm the entities and schema matched.
- Dropped the column and constraint V7 had added and deleted V7's row from the history table, returning to a V6 state with three posts.
- On restart,
Migrating to version 7appeared, all three posts were filled withKO,EN, and no body, category, or status data was lost.
Only after that passed did I deploy to production, where the result was the same. Today there is also a separate test task that applies migrations to a real, disposable PostgreSQL instance.
After moving to Flyway
ddl-auto=update | Flyway + validate | |
|---|---|---|
| Who changes the schema | Hibernate, comparing entities and schema | Numbered SQL written by a person |
| Filling existing rows | No place to decide | Written directly in SQL (add → fill → require) |
| On failure | Can boot with a partially changed schema | Boot stops on a failed migration or check |
| Versioned change history | Must be tracked separately | SQL files in Git plus the history table |
| What it doesn't check | — | Not every constraint is covered |
Moving to Flyway with zero production posts meant there was no post data to migrate. Later schema changes went through SQL; the records show no further need to wipe the production database.
The next post is about "passing tests ≠ a booting app." It covers the problems that only showed up in production while every test passed, including the quietly skipped Flyway I postponed above.
Sources
-
[S1] Database Initialization | Spring Boot Docs | Checked 2026-11-02 | https://docs.spring.io/spring-boot/how-to/data-initialization.html
↩ -
[S2] Modifying Tables | PostgreSQL | Checked 2026-11-02 | https://www.postgresql.org/docs/current/ddl-alter.html
↩ -
[S3] Baseline | Flyway | Checked 2026-11-02 | https://documentation.red-gate.com/fd/baseline-277578867.html
↩ -
[S4] AbstractSchemaValidator (7.2) | Hibernate ORM | Checked 2026-11-02 | https://raw.githubusercontent.com/hibernate/hibernate-orm/7.2/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/AbstractSchemaValidator.java
↩ -
[S5] Spring Boot 4.0 Migration Guide | Checked 2026-11-02 | https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-4.0-Migration-Guide
↩
About these records
This post draws on my record of the boot failure in late June 2026, my notes from adopting Flyway on July 1, and the migration SQL that is in the repository today. Error messages are copied from what the notes preserved, and I rechecked Spring Boot's behavior against the official docs. Some SQL is excerpted from the real files and marked as such.
Report an error or share feedback
Open a draft with this article’s title and URL. Review the message and recipient before sending.
Open email draftIf no email app opens, copy these details into your usual email service.
Related posts
Backend & Infrastructure Spring Boot 4.1 Jackson Settings: Comparing Duplicate JSON Keys and BigDecimal Output
Compare two Jackson settings in the same Spring Boot 4.1.0 app using the recorded six tests and six HTTP requests for duplicate JSON keys and BigDecimal notation.
Backend & Infrastructure A Spring Backend Learning Roadmap: Seven Skills to Demonstrate with a Small Service
A fictional reading-log service connects practice tasks and completion criteria for business rules, HTTP, transactions, permissions, queries, tests, and operations.