tezvyn:

How do you add a non-null column with a default in Room?

Source: developer.android.comintermediate

WHAT IT TESTS: SQLite ALTER TABLE semantics and Room Migration wiring. ANSWER OUTLINE: Bump version; run ALTER TABLE ADD COLUMN with NOT NULL DEFAULT in a Migration; register via addMigrations. RED FLAG: Dropping tables or omitting DEFAULT on existing rows.

WHAT IT TESTS: Whether you understand SQLite ALTER TABLE constraints and how Room's Migration API preserves user data during schema changes. ANSWER OUTLINE: First, increment the database version and define a Migration object that executes ALTER TABLE ADD COLUMN with NOT NULL and a DEFAULT clause. Second, register that migration in your RoomDatabase Builder using addMigrations so Room runs it on upgrade. RED FLAG: Proposing fallbackToDestructiveMigration, dropping the table, or adding NOT NULL without DEFAULT, which destroys data or crashes.

Read the original → developer.android.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

How do you add a non-null column with a default in Room? · Tezvyn