Sunday, December 22, 2019

Android Room Database Subsequent Insert Failed Due to Broken AutoIncrement

Room DB has managed to abstract away complicated SQL statements which is pretty nice. But as with other new things, it takes a while to get used to.

Problem

It all starts with an entity such as the following:
@Entity
data class Item(
    @PrimaryKey(autoGenerate = true) val id: Int = Int.MIN_VALUE,
    @ColumnInfo val name: String?
}

and my Dao has the following function:
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(item: Item)

The first insert went well, but I noticed my subsequent insert failed. 

Solution

For somewhat reason, it tried to assign the same value as Id. After few trial and error, I managed to fix it by changing the Int.MIN_VALUE to 0. So the entity class becomes:

@Entity
data class Item(
    @PrimaryKey(autoGenerate = true) val id: Int = 0,
    @ColumnInfo val name: String?
}

No comments:

Post a Comment