[{"data":1,"prerenderedAt":91},["ShallowReactive",2],{"/2025/11/09-android-room-database-subsequent-insert-failed-due-to-broken-autoincrement":3},{"id":4,"title":5,"body":6,"date":82,"description":49,"extension":83,"meta":84,"navigation":85,"path":86,"robots":87,"seo":88,"stem":89,"__hash__":90},"posts/2025/11/09 Android-Room-Database-subsequent-insert-failed-due-to-broken-AutoIncrement.md","09 Android Room Database Subsequent Insert Failed Due To Broken AutoIncrement",{"type":7,"value":8,"toc":79},"minimark",[9,18,25,28,31,33,36,50,53,61,63,66,68,71],[10,11,13],"post-title",{":date":12},"date",[14,15,17],"h1",{"id":16},"android-room-database-subsequent-insert-failed-due-to-broken-autoincrement","Android Room Database Subsequent Insert Failed Due to Broken AutoIncrement",[19,20,21],"notes",{},[22,23,24],"p",{},"This is a repost from my old blog. First posted in 12/22/2019.",[26,27],"br",{},[22,29,30],{},"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.",[26,32],{},[22,34,35],{},"It all starts with an entity such as the following:",[37,38,39],"code-block",{},[40,41,46],"pre",{"className":42,"code":44,"language":45},[43],"language-text","@Entity\ndata class Item(\n    @PrimaryKey(autoGenerate = true) val id: Int = Int.MIN_VALUE,\n    @ColumnInfo val name: String?\n}\n","text",[47,48,44],"code",{"__ignoreMap":49},"",[22,51,52],{},"and my Dao has the following function:",[37,54,55],{},[40,56,59],{"className":57,"code":58,"language":45},[43],"@Insert(onConflict = OnConflictStrategy.IGNORE)\nsuspend fun insert(item: Item)\n",[47,60,58],{"__ignoreMap":49},[26,62],{},[22,64,65],{},"The first insert went well, but I noticed my subsequent insert failed.",[26,67],{},[22,69,70],{},"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:",[37,72,73],{},[40,74,77],{"className":75,"code":76,"language":45},[43],"@Entity\ndata class Item(\n    @PrimaryKey(autoGenerate = true) val id: Int = 0,\n    @ColumnInfo val name: String?\n}\n",[47,78,76],{"__ignoreMap":49},{"title":49,"searchDepth":80,"depth":80,"links":81},2,[],"2025-11-09T00:00:00.000Z","md",{},true,"/2025/11/09-android-room-database-subsequent-insert-failed-due-to-broken-autoincrement",null,{"title":5,"description":49},"2025/11/09 Android-Room-Database-subsequent-insert-failed-due-to-broken-AutoIncrement","xvmClhPbsQLItrwL_dUV4b_9iT7pkVglyj0LJO_id-I",1785167452602]