Nontas Papadopoulos
Nontas Papadopoulos

Reputation: 21

Room migration fails even when I specify default value using @ColumnInfo

I have this entity and today I added one more field at the bottom: isClient: Boolean

@Entity(tableName = "user")
data class UserDataEntity(
    @PrimaryKey
    val id: String,
    val username: String,
    val firstName: String?,
    val lastName: String?,
    val language: Language,
    @Embedded(prefix = "globalRole_") val globalRole: UserRoleDataEntity?,
    @Embedded(prefix = "projectRole_") val projectRole: UserRoleDataEntity?,
    val selectedProjectId: String?,
    @ColumnInfo(defaultValue = "0")
    val isClient: Boolean
)

data class UserRoleDataEntity(
    val id: String,
    val name: String
)

Here is my Database:

@Database(
    entities = [
        ScanConfigurationDataEntity::class,
        ScanFlowStepDataEntity::class,
        AuditingSettingsDataEntity::class,
        ZoneCodeConfigurationDataEntity::class,
        ZoneCheckInDataEntity::class,
        SessionDataEntity::class,
        ScanDataEntity::class,
        VerifySelectionsDataEntity::class,
        ProjectDataEntity::class,
        UserDataEntity::class,
        SettingsDataEntity::class,
        ZoneLinkingStepDataEntity::class,
        ZoneLinkDataEntity::class,
        VerifyConfigurationDataEntity::class
    ],
    autoMigrations = [
        AutoMigration(from = 1, to = 2),
        AutoMigration(from = 2, to = 3),
        AutoMigration(from = 3, to = 4),
        AutoMigration(from = 4, to = 5),
        AutoMigration(from = 5, to = 6),
        AutoMigration(from = 6, to = 7, spec = Migrations.RenameUserLanguage::class),
        AutoMigration(from = 7, to = 8),
        AutoMigration(from = 8, to = 9),
        AutoMigration(from = 9, to = 10),
        AutoMigration(from = 10, to = 11),
        AutoMigration(from = 11, to = 12),
        AutoMigration(from = 12, to = 13),
        AutoMigration(from = 13, to = 14, spec = Migrations.Migrate13to14::class),
        AutoMigration(from = 14, to = 15, spec = Migrations.Migrate14to15::class),
        AutoMigration(from = 15, to = 16),
        AutoMigration(from = 16, to = 17, spec = Migrations.Migrate16to17::class),
        AutoMigration(from = 17, to = 18, spec = Migrations.Migrate17to18::class)
    ],
    version = 18,
)

And this is my Migrations file (the previous migrations where removed)

class Migrations {

    @RenameColumn(tableName = "user", fromColumnName = "preferredLanguage", toColumnName = "language")
    class RenameUserLanguage : AutoMigrationSpec

    class Migrate13to14: AutoMigrationSpec
    class Migrate14to15: AutoMigrationSpec
    class Migrate16to17: AutoMigrationSpec
    class Migrate17to18: AutoMigrationSpec

    companion object {
        val allMigrations = arrayOf<Migration>(

            object : Migration(17, 18) {
                override fun migrate(database: SupportSQLiteDatabase) = with(database) {
                    database.execSQL("""
                            CREATE TABLE `user_new` (
                                `id` TEXT NOT NULL, 
                                `username` TEXT NOT NULL, 
                                `firstName` TEXT, 
                                `lastName` TEXT, 
                                `language` TEXT NOT NULL, 
                                `selectedProjectId` TEXT, 
                                `globalRole_id` TEXT, 
                                `globalRole_name` TEXT, 
                                `projectRole_id` TEXT, 
                                `projectRole_name` TEXT, 
                                `isClient` INTEGER NOT NULL DEFAULT 0, 
                                PRIMARY KEY(`id`)
                            )
                        """)

                    database.execSQL("""
                            INSERT INTO user_new (
                                id, username, firstName, lastName, language, 
                                selectedProjectId, globalRole_id, globalRole_name, 
                                projectRole_id, projectRole_name, isClient
                            )
                            SELECT 
                                id, username, firstName, lastName, language, 
                                selectedProjectId, globalRole_id, globalRole_name, 
                                projectRole_id, projectRole_name, 0 AS isClient
                            FROM user
                        """)
                    execSQL("DROP TABLE user")
                    execSQL("ALTER TABLE user_new RENAME TO user")

                }

            }
        )
    }
}

I have tried many queries, I`m not really good at SQL but whatever I do I`m getting this error:

error: New NOT NULL column'isClient' added with no default value specified. Please specify the default value using @ColumnInfo.
public abstract class AppDatabase extends androidx.room.RoomDatabase {

I have seen many posts. Most of them suggest to add the annotation @ColumnInfo.

Upvotes: 0

Views: 27

Answers (0)

Related Questions