CodexBloom - Programming Q&A Platform

How to implement guide with room database migration scenarios on android 14 with custom type converters

👀 Views: 96 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-05
android room database migration kotlin Kotlin

I'm a bit lost with I'm stuck on something that should probably be simple. I'm building a feature where I've been struggling with this for a few days now and could really use some help... I've been banging my head against this for hours. I'm working with a question with Room database migrations when trying to upgrade from version 1 to version 2 on Android 14. My database has a custom type, a `List<String>`, which I store using a TypeConverter. The migration fails with the behavior: `SQLiteException: no such column: my_table.my_custom_list`. I've defined the TypeConverter in my `@Database` annotation, but it seems like Room want to find the column during the migration. Here's the relevant part of my code: ```kotlin @Entity(tableName = "my_table") data class MyEntity( @PrimaryKey val id: Int, @TypeConverter val myCustomList: List<String>? ) class Converters { @TypeConverter fun fromStringList(value: List<String>): String { return value.joinToString(separator = ",") } @TypeConverter fun toStringList(value: String): List<String> { return value.split(",") } } @Database(entities = [MyEntity::class], version = 2) @TypeConverters(Converters::class) abstract class AppDatabase : RoomDatabase() { abstract fun myEntityDao(): MyEntityDao() } ``` For the migration, I'm using the following code: ```kotlin val MIGRATION_1_2 = object : Migration(1, 2) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("CREATE TABLE new_my_table (id INTEGER PRIMARY KEY NOT NULL, my_custom_list TEXT)") database.execSQL("INSERT INTO new_my_table (id, my_custom_list) SELECT id, my_custom_list FROM my_table") database.execSQL("DROP TABLE my_table") database.execSQL("ALTER TABLE new_my_table RENAME TO my_table") } } ``` I've ensured that the database schema is updated correctly, and I can see that the new column `my_custom_list` exists after migration. However, during runtime, it throws the `no such column` behavior when trying to access the data. I've tried clearing the app's data and reinstalling, but the scenario continues. Any help on why the migration isn't recognizing the new column would be greatly appreciated! I'm working on a CLI tool that needs to handle this. What am I doing wrong? This is happening in both development and production on Debian. What's the correct way to implement this? Any ideas how to fix this? Thanks for your help in advance!