0

i have two entities with auto generated primary keys, so i want to get User id to put it in Settings userId, is there simple way to do this or i should manually check last row in my table to get it's id?

@Entity(tableName = "users")
data class User(
    @PrimaryKey(autoGenerate = true) var id: Int = 0,
    var email: String,
    var password: String,
)

@Entity(
    tableName = "settings",
    foreignKeys = [ForeignKey(
        entity = User::class,
        parentColumns = arrayOf("id"),
        childColumns = arrayOf("user_id"),
        onDelete = ForeignKey.NO_ACTION
    )]
)
data class Settings(
    @PrimaryKey(autoGenerate = true) var id: Int = 0,
    var firstSettings: Boolean = false,
    var secondSettings: Boolean = false,
    var thirdSettings: Boolean = false,
    @ColumnInfo(name = "user_id") var userId: Int,
)
  • Does this answer your question? [Android Room - Get the id of new inserted row with auto-generate](https://stackoverflow.com/questions/44364240/android-room-get-the-id-of-new-inserted-row-with-auto-generate), [How to get the id of inserted row in android room databse in Kotlin?](https://stackoverflow.com/questions/68974305/how-to-get-the-id-of-inserted-row-in-android-room-databse-in-kotlin) – forpas Feb 12 '23 at 20:42

0 Answers0