How does Room store an enum status property when you write no converter, and when would you switch that built-in converter off?
answer
- a converter Room writes itself
- name, not ordinal
- what a rename does to old rows
- a flag with three states
basics
~10 sRoom's built-in enum converter stores the constant's name() as text, so renaming a constant breaks old rows. Teams disable it with BuiltInColumnTypeConverters(enums = DISABLED) (Room 2.x: BuiltInTypeConverters) to force an explicit, stable encoding.
solid answer
~40 sIf no app converter handles an enum, Room generates one that writes `name()` into a TEXT column and maps it back on read; my own converter takes priority over it. Reordering constants is safe, but renaming one leaves old rows holding a name no constant matches, and reading them fails. To make that a build-time decision I disable the built-in: in Room 3.0, `@ColumnTypeConverters(..., builtInColumnTypeConverters = BuiltInColumnTypeConverters(enums = BuiltInColumnTypeConverters.State.DISABLED))`; in Room 2.x, `builtInTypeConverters = BuiltInTypeConverters(enums = ...DISABLED)` on `@TypeConverters`. The flag follows registration scopes, defaults to `INHERITED` and means enabled if no scope sets it. With it off, every enum needs an explicit converter, typically a stable integer code.
code
kotlin · 27 linesimport androidx.room3.BuiltInColumnTypeConverters
import androidx.room3.ColumnTypeConverter
import androidx.room3.ColumnTypeConverters
import androidx.room3.Database
import androidx.room3.RoomDatabase
enum class EventStatus(val code: Int) { DRAFT(0), PUBLISHED(1), CANCELLED(2) }
object EventStatusConverters {
@ColumnTypeConverter
fun toCode(status: EventStatus): Int = status.code
@ColumnTypeConverter
fun fromCode(code: Int): EventStatus = EventStatus.entries.first { it.code == code }
}
@Database(entities = [Event::class], version = 1)
@ColumnTypeConverters(
InstantConverters::class,
EventStatusConverters::class,
builtInColumnTypeConverters = BuiltInColumnTypeConverters(
enums = BuiltInColumnTypeConverters.State.DISABLED,
),
)
abstract class EventDatabase : RoomDatabase() {
abstract fun eventDao(): EventDao
}go deeper
Remember that Room stores an enum's name as text by default and that your own converter replaces that behaviour.
Explain the priority between built-in and app converters, the ENABLED, DISABLED and INHERITED states, and why a rename breaks old rows but a reorder does not.
Diagnose a read failure after an enum refactor, and plan the migration that moves an existing name column to stable codes.
Decide whether the team disables built-in enum conversion database-wide so every persisted enum gets a reviewed, rename-proof encoding.
## What Room does with an enum by default Since Room 2.3, an enum property needs no converter of yours. When no app converter handles that enum, Room **generates one**: it writes the constant's `name()` into a TEXT column and, on read, maps the stored string back to the constant with that name. Room 2.4 added the same kind of built-in support for `java.util.UUID`, stored as its `ByteArray` form, and the built-in set also covers `java.nio.ByteBuffer`. Two rules decide which converter Room picks for an enum: - **Your converter wins.** Built-in converters are enabled by default but have *lower priority* than user-provided ones. If you register a `@ColumnTypeConverter` pair for `EventStatus` (Room 2.x: `@TypeConverter`), Room uses yours. - **The built-in one is a fallback, not a format you chose.** Nobody on the team picked "store the name as text"; Room did, and the stored strings now depend on how the constants are spelled. ## What the default costs you later Storing `name()` has one good property and one dangerous one: | Change to the enum | Effect on existing rows | |---|---| | reorder the constants | none: the name, not the ordinal, is stored | | add a new constant | none for existing rows | | rename `ACTIVE` to `LIVE` | old rows still say `ACTIVE`, which no longer matches any constant, so reading those rows fails | | delete a constant | the same failure for rows that hold it | A rename is an innocent refactor in Kotlin and a data problem on every phone that already stored the old name. Room's compile-time checks cannot catch it, because the column is just TEXT to them. ## Turning the built-in converter off The registration annotation carries a flag object for the built-ins. In **Room 3.0**: ```kotlin @ColumnTypeConverters( EventStatusConverters::class, builtInColumnTypeConverters = BuiltInColumnTypeConverters( enums = BuiltInColumnTypeConverters.State.DISABLED, ), ) ``` In **Room 2.x** (since 2.4.0) the same thing is `@TypeConverters(..., builtInTypeConverters = BuiltInTypeConverters(enums = BuiltInTypeConverters.State.DISABLED))`. Each flag (`enums`, `uuid`, `byteBuffer`) takes one of three states: 1. `ENABLED`: Room may use the built-in converter in this scope. 2. `DISABLED`: Room may not. 3. `INHERITED`, the default: take the value from the enclosing scope, and behave as `ENABLED` if no scope ever set it. Because the flags follow the same scopes as registration, you can disable the enum converter for the whole database on the `@Database` class and still re-enable it for one legacy entity. ## The other built-ins: UUID and ByteBuffer The same flags object covers two more types: - **`uuid`**: `java.util.UUID` is stored as its `ByteArray` form, so the column is a BLOB. It is compact and compares exactly, but it is unreadable in a database inspector and awkward to type into an ad hoc query. Teams that want readable ids register their own converter to the 36-character string form, which then takes priority over the built-in one. - **`byteBuffer`**: `java.nio.ByteBuffer` is stored as its bytes. Both default to `INHERITED`, which means enabled unless a scope says otherwise, and both can be turned off per scope exactly like `enums`. ## Why a team would switch it off Disabling the enum converter turns a silent default into a **build-time decision**. With it off, any enum property without an explicit converter fails to compile, so every stored enum gets an encoding someone chose. Typical choices: - a **stable code** field on the enum (`DRAFT(0)`, `PUBLISHED(1)`) stored as INTEGER, which survives renames; - an explicit string value per constant, decoupled from the Kotlin name; - a deliberate fallback constant such as `UNKNOWN` for values written by a newer app version. Keep the converter **two-way**. Room 2.3's release notes flagged a known issue: with only a one-way converter for an enum, Room might pick the built-in string-to-enum converter instead, and the documented fix is a two-way converter. ## Changing the encoding of an existing column Switching an existing TEXT column of names to INTEGER codes changes the schema and the data, so it needs a database version bump and a migration that rewrites the stored values; that mechanism belongs to the lesson on Room migrations. For a new column, choosing the explicit encoding up front costs nothing.
- Can you disable the built-in enum converter for the whole database but keep it for one legacy entity?Yes. Set `enums = State.DISABLED` on the `@Database` class's registration annotation and `enums = State.ENABLED` on the legacy `@Entity`'s own `@ColumnTypeConverters` (Room 2.x: `@TypeConverters`). Flags left at `INHERITED` take the value of the enclosing scope, so the narrower scope's explicit setting applies there.
- Which other types does Room convert by itself, and how are they stored?`java.util.UUID`, stored as its `ByteArray` form, and `java.nio.ByteBuffer`. Both have their own flags, `uuid` and `byteBuffer`, on the same built-in flags object, and like the enum converter they are enabled by default and lose to any converter you register for the type.
- Room stores enums by ordinal, so reordering constants corrupts data
- By default, Room cannot store an enum without a hand-written converter
- The built-in enum converter overrides a converter you registered
- Renaming an enum constant is safe because Room migrates old values
- Disabling the built-in converter makes Room fall back to ordinals
Room added the built-in enum converter in 2.3 and the switch for it in 2.4.0; Room 3.0 names the switch builtInColumnTypeConverters with BuiltInColumnTypeConverters, Room 2.x builtInTypeConverters with BuiltInTypeConverters.