Android Room Database Subsequent Insert Failed Due to Broken AutoIncrement

Sunday, November 9, 2025
This is a repost from my old blog. First posted in 12/22/2019.

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.


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.


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?
}