How to get the inserted row in Kotlin Exposed?
Asked Answered
M

2

7

Here is how we get the id of the added row (source):

val munichId = Cities.insert {
    it[name] = "Munich"
} get Cities.id

What about getting the whole row? I use map for select operations but it does not work for insert.

Meiny answered 21/6, 2021 at 10:26 Comment(0)
M
10
val row = Cities.insert {
    it[name] = "Munich"
}.resultedValues!!.first()
Matamoros answered 21/6, 2021 at 12:52 Comment(0)
S
0

Another option is insertReturning method from https://github.com/JetBrains/Exposed/pull/2060

Items.insertReturning(listOf(Items.id, Items.name)) {
    it[name] = "B"
    it[price] = 200.0
}.map {
    it[Items.id].value to it[Items.name] 
}
Spank answered 26/6 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.