How to save enum in database as string
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

