How to make a primary key auto-increment in Room (Android/Kotlin)?
Hello everyone! I'm a beginner Android developer in Kotlin and I'm trying to figure out the Room library. I have an Entity for storing notes. I want the id field to automatically increment when inserting a new record, like in regular SQLite databases with `AUTOINCREMENT`. But I can't get it to work.\n\nHere's my code:\n\n```kotlin\n@Entity(tableName = "notes")\ndata class Note(\n @PrimaryKey\n val id: Int = 0,\n val title: String,\n val content: String\n)\n```\n\nWhen I insert a new note through DAO, I specify id = 0, but Room throws an error that such an id already exists (or a conflict). I tried setting `autoGenerate = true`, but then the compiler complains that `autoGenerate` can only be used with `Long` or `Int`. I've read several answers on Stack Overflow,
No answers yet.
Log in to leave an answer