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

SQLite ALTER TABLE semantics and Room Migration wiring.
Bump version; run ALTER TABLE ADD COLUMN with NOT NULL DEFAULT in a Migration; register via addMigrations.
Dropping tables or omitting DEFAULT on existing rows.
What's really being asked
This question probes whether you know the boundary between SQLite's native ALTER TABLE capabilities and Room's Migration abstraction. The interviewer wants to see that you understand SQLite can add a non-null column only when a default is supplied, that you know how to express this in Room's Migration class, and that you treat user data as immutable during upgrades.
The full answer
Four things in order. First, increment the database version number in your RoomDatabase annotation. Second, create a Migration instance for the version pair, override migrate, and inside it call db.execSQL with ALTER TABLE my_table ADD COLUMN new_column INTEGER NOT NULL DEFAULT 0. Third, register that migration by passing it to Room.databaseBuilder via addMigrations. Fourth, mention that because SQLite assigns the default value to every existing row automatically, no manual backfill is required and zero data is lost.
The mistakes people make
A major red flag is suggesting fallbackToDestructiveMigration or dropping the table and recreating it, because both erase user data. Another is proposing to add a NOT NULL column without a DEFAULT clause, which SQLite rejects when rows already exist. Some candidates also forget to register the Migration object in the builder, which causes Room to throw an IllegalStateException at runtime. Finally, describing a complex table-rebuild workflow for this simple additive change reveals unfamiliarity with SQLite ALTER TABLE semantics.
What usually comes next
The interviewer may ask what happens if you need to add a non-null column without any default value; the correct response is that SQLite forbids it, so you must perform a table rebuild by creating a new table, copying existing data with an inserted placeholder value, dropping the old table, and renaming the new one. They may also ask how to test migrations, in which case you should mention MigrationTestHelper and writing an instrumentation test that validates the schema and data after the migration runs. Another follow-up is Room's auto-migration feature; note that Room can generate simple migrations automatically if you export schemas and use AutoMigration, but manual Migrations are still required for changes that Room cannot infer.
A concrete example
Suppose your app has a User table with id and name, and you need to add age INTEGER NOT NULL DEFAULT 0 in version 2. You would annotate the entity field with ColumnInfo and set its defaultValue attribute, bump the database version to 2, then write val MIGRATION_1_2 = object : Migration(1, 2) { override fun migrate(db: SupportSQLiteDatabase) { db.execSQL("ALTER TABLE User ADD COLUMN age INTEGER NOT NULL DEFAULT 0") } } and pass it to addMigrations(MIGRATION_1_2). Existing users keep their rows and age is backfilled to zero automatically.
Interview question
Which step sequence correctly adds a non-null column to an existing Room database without losing data?
- a.Annotate the new field with defaultValue, increment the version, and let Room automatically migrate without a Migration object
- b.Increment the version, run ALTER TABLE ADD COLUMN with NOT NULL DEFAULT in a registered Migration, and let SQLite backfill existing rowsCorrect
- c.Execute ALTER TABLE ADD COLUMN with NOT NULL and no DEFAULT inside a Migration, then backfill rows with Kotlin code after the migration
- d.Enable fallbackToDestructiveMigration, add the column to the entity, and let Room recreate the table on upgrade
Why? this is the answer
SQLite requires a DEFAULT value when adding a non-null column to a table that already has rows, and Room requires the migration to be registered via addMigrations. Option C is tempting because it uses a Migration, but SQLite rejects the ALTER statement outright if no DEFAULT is provided.
Just read this? Test yourself on what you have been reading.
Read the original → developer.android.com
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.
See open roles