Declaration in IntIdTable: val userId = reference("userId", DB.UsersTable.id)
Declaration in IntEntity: var user by DB.User referencedOn EmailVerifications.userId
Works as expected in 0.11.2 and 0.12.1, but breaks in 0.12.2 with following:
Exception in thread "main" java.lang.IllegalStateException: Unexpected value of type Int: 26 of org.jetbrains.exposed.dao.EntityID
at org.jetbrains.exposed.sql.IntegerColumnType.valueFromDB(ColumnType.kt:131)
at org.jetbrains.exposed.sql.ResultRow.get(Query.kt:35)
at org.jetbrains.exposed.dao.EntityCache.flushInserts$exposed(Entity.kt:467)
at org.jetbrains.exposed.dao.EntityCache.flush(Entity.kt:405)
at org.jetbrains.exposed.dao.EntityCache.flush(Entity.kt:397)
at org.jetbrains.exposed.sql.Transaction.flushCache(Transaction.kt:85)
at org.jetbrains.exposed.sql.Transaction.commit(Transaction.kt:62)
at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:105)
at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:75)
at Verifier.verifyRequest(Verifier.kt:93)
at Verifier.main(Verifier.kt:90)
Through the debugger I see that the type of value of value at ColumntType.kt:128 is EntityID instead of expected Int or Number while value._value is Int 26.
Seems to be a bug
As I don't have a step to reproduce, I was able to make the only possible fix, so please test it after a 0.13.1 release and leave feedback.
This issue seems to be related to another documentation issue - when using a reference like in the following example, you need to reference the table itself, not its id:
// CORRECT
object Data : Table("data") {
val user = reference("user", Users, onDelete = CASCADE).primaryKey()
…
}
// WRONG
object Data : Table("data") {
val user = reference("user", Users.id, onDelete = CASCADE).primaryKey()
…
}
If that's indeed a correct assumption, I'll add that gotcha to the Wiki (cc @Tapac).
Feeling sorry for long response
No, in 0.13.1..3 I'm getting the same Exception.
The proposal by @Maxr1998 fixes the issue, thank you. Pretty unobvious, so when I should use the reference(name: String, refColumn: Column
Clarification in Wiki will be damn good, but it seems to me that this trap needs exception with a comprehensive message (enhancement?)
Yes, I've been dancing around this for days! This must be mentioned in the Wiki.
+1
Just spent a few hours wrestling with this. Documentation fix appears to resolve the issue.
@lavalamp- , could you share you code which leads to IllegalStateException to help with fix?
@Tapac thank you for the response and apologies for not providing more details first.
Happy to provide another example but the post above by @Maxr1998 is the exact case of how I ran into the issue (reference on an ID column rather than the table itself).
Fixed in master. WIll be released soon.
Does that mean both versions work now? Or does only referencing the id field work/compile?
Wait, would this be a breaking change? Or would both methods be supported together?
There will be another reference/optReference functions with Column<EntityID<*>> param.
It shouldn't brake anything.
Most helpful comment
Feeling sorry for long response) alternative? Only for referencing by non-primary keys?
No, in 0.13.1..3 I'm getting the same Exception.
The proposal by @Maxr1998 fixes the issue, thank you. Pretty unobvious, so when I should use the reference(name: String, refColumn: Column
Clarification in Wiki will be damn good, but it seems to me that this trap needs exception with a comprehensive message (enhancement?)