https://kotlinlang.org logo
#exposed
Title
# exposed
j

John Pena

03/26/2021, 2:44 PM
when i call this code, i get an
IllegalStateException: No transaction in context
. anyone know what i’m doing wrong here?
Copy code
val breakdownDimensions = transaction {
                Dimension.find {
                    Dimensions.id inList breakdownIds
                }
            }

            for (dimension in breakdownDimensions) {
                println(dimension.id)
            }
e

Endre Deak

03/26/2021, 3:40 PM
did you wrap this into
transaction { }
?
j

John Pena

03/26/2021, 3:41 PM
there’s a transaction in the code i posted
i’m not entirely clear on what the issue was, but wrapping all of the code that uses my database types in a transaction did fix my problem
e

Endre Deak

03/26/2021, 9:44 PM
yeah in order to be able to use them, you must be inside a transaction
j

Joel

03/29/2021, 7:13 PM
@John Pena I believe
Dimension.find
returns a
SizedIterable
which hasn't actually queried yet. The reason is that you could be asking for count, or existence, something that doesn't require the query to be completed in full. The query is actually occurring when you access the iterator using
dimension in breakdownDimensions
. The fix would be to use:
Copy code
Dimension.find { Dimensions.id inList breakdownIds }.toList()
1
j

John Pena

03/29/2021, 7:14 PM
helpful, thanks @Joel!
👍 1
8 Views