How to insert into two tables and return two PKs from transaction. See my sample code. I am getting return statement not allowed error.
Copy code
suspend fun insertIntoTwoTables(x: Int, y: Int) {
Database.dbQuery{
transaction{
var a = Numbers.insert { it[Numbers.num] = x }
val aId = a.resultedValues?.get(Numbers.id) ?.let { it.toLong() }
val b = Strings.insert {
it[Strings.str] = y.toString()
it[Strings.numId] = aId
}
val bId = b.resultedValues?.get(Strings.id) ?.let { it.toLong() }
return Pair(aId, bId)
}
}
}
a
AdamW
02/03/2024, 11:03 AM
You can’t have an unqualified return inside a non-inline lambda. The outer function has a return type of
Unit
so this couldn’t work anyway.
No need to open another transaction inside your
dbQuery
either.
Copy code
fun thing(): Int = transaction {
10
} // ok
fun thing() {
transaction {
return 10
}
} // error
fun thing(): Int {
transaction {
10
} // error
}
fun thing(): Int {
return transaction {
10
}
} // ok
👍🏾 1
m
Manas Marthi
02/05/2024, 6:00 AM
thank you. I guess
newSuspendedTransaction
call is taking care of opening the transaction. Isn't it? Is it possible to call a stored procedure using Exposed?
a
AdamW
02/06/2024, 5:07 PM
Not sure if there is DSL for it, but you can always use
exec
to run arbitrary SQL 👍 And yes, it opens a transaction
👍🏾 1
m
Manas Marthi
02/06/2024, 6:04 PM
that's good to know. could you please give a sample of using exec to call a stored procedure or run a update query or select query?