How to save enum in database as string
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Saving an enum as a string in the database is usually safer than saving its numeric ordinal. String storage makes the data readable and avoids silent corruption when enum members are reordered later in code.
Why String Storage Is Safer Than Ordinals
Suppose you start with this enum:
If an ORM stores ordinals, NEW might become 0, ACTIVE might become 1, and ARCHIVED might become 2. That looks compact, but it is fragile. If someone later inserts PAUSED between NEW and ACTIVE, the old stored numbers now point to the wrong meaning.
String storage avoids that problem because the database stores NEW, ACTIVE, or ARCHIVED directly. Reordering the enum in code no longer changes existing records.
Map Enums to Strings in Common ORMs
In JPA, use EnumType.STRING explicitly:
In Entity Framework Core, configure a string conversion:
In SQLAlchemy, define the enum with explicit string values:
The implementation varies by framework, but the design goal is the same: keep stored values stable and readable.
Plan for Renames and Legacy Data
String storage is safer, but it is not magic. If you rename an enum constant that is already persisted, the database still contains the old text. That means renames need a migration plan.
A safe rollout usually looks like this:
- add support for both old and new names temporarily
- migrate stored rows to the new value
- remove the backward-compatibility alias
If you need database values that differ from code identifiers, use an explicit converter instead of relying on the enum constant name itself.
That pattern gives you freedom to keep storage values stable even if enum constant names need refactoring.
Common Pitfalls
The biggest mistake is accepting the ORM default without checking whether it stores ordinals. Many frameworks choose the compact representation unless you configure them otherwise.
Another common issue is renaming enum constants without a data migration. String storage protects you from reordering, not from changing the stored text itself.
People also underestimate schema design. If the column is too short, future enum additions can fail unexpectedly. Give the text column reasonable headroom.
Finally, do not use enum string storage as an excuse to skip validation. Unknown values should still be rejected or handled explicitly when reading from the database.
If the enum crosses service boundaries, document the persisted string values as part of the contract. That prevents accidental changes during refactors in one codebase.
Summary
- Saving enums as strings is usually safer than saving numeric ordinals.
- String storage prevents data drift when enum members are reordered in code.
- Configure your ORM explicitly, because defaults are not always safe.
- Renaming persisted enum values still requires a migration plan.
- Use explicit converters when storage values should stay stable across code refactors.
Related reading
- How to save MySQL query output to excel or .txt file?
- How to schedule a stored procedure in MySQL
- how to search for a given word from a huge database?
- How to search JSON data in MySQL?
- How to see full query from SHOW PROCESSLIST?
- How to see indexes for a database or table in MySQL?
- How to see log files in MySQL?
- How to select a column name with a space in MySQL

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.