ufoss-org / kotysa

The idiomatic way to write type-safe SQL in Kotlin
The Unlicense
116 stars 1 forks source link

java.lang.ClassCastException on composite primary key #96

Closed Metis-Adrastea closed 2 years ago

Metis-Adrastea commented 2 years ago
data class User(val name: String, val age: Int)

object Users : PostgresqlTable<User>("users") {
    val name = text(User::name)
    val age = integer(User::age)

    init {
        primaryKey(name, age)
    }
}
Exception in thread "main" java.lang.ExceptionInInitializerError
    at MainKt.main(Main.kt:28)
    at MainKt.main(Main.kt)
Caused by: java.lang.ClassCastException: [Lorg.ufoss.kotysa.Column; cannot be cast to [Lorg.ufoss.kotysa.DbColumn;
    at Users.<clinit>(Main.kt:15)
    ... 2 more

java.lang.ClassCastException on composite primary key with different column types. Full example https://github.com/Metis-Adrastea/kotysa-composite-pk

pull-vert commented 2 years ago

Hi @Metis-Adrastea , thanks a lot for the feedback !!

You have to declare a value for the composite primary key :

object Users : PostgresqlTable<User>("users") {
    val name = text(User::name)
    val age = integer(User::age)
    val pk = primaryKey(name, age)
}

I will add this example to the Kotysa table mapping doc

Metis-Adrastea commented 2 years ago

Hi @pull-vert! Thanks for the quick response, but this option doesn't work either. The same error occurs.

Exception in thread "main" java.lang.ExceptionInInitializerError
    at MainKt.main(Main.kt:25)
    at MainKt.main(Main.kt)
Caused by: java.lang.ClassCastException: [Lorg.ufoss.kotysa.Column; cannot be cast to [Lorg.ufoss.kotysa.DbColumn;
    at Users.<clinit>(Main.kt:13)
    ... 2 more

I have committed these changes to the test repository.

pull-vert commented 2 years ago

@Metis-Adrastea thanks again for submitting this issue, and providing a reproduction repository.

I just released Kotysa 2.1.2 with that Fix, it should be on Maven Central now (or very soon)

Thanks also for the init nice syntax for composite primary key, more elegant than my unused value declaration ! :slightly_smiling_face:

Metis-Adrastea commented 2 years ago

@pull-vert Thanks a lot, very cool!