Skip to content

[Postgres] enum ClassCastException #609

@abubics

Description

@abubics

Lib versions checked: 0.15.1, 0.16.2

Hi :) I've been looking through the existing (mostly closed) issues, but they seem old and only partially related.

Using the DAO API, and following examples in the docs, I've had enums working fine, until trying to set a column (creating and reading records has been ok).

Now, after setting a column value, the transaction flush throws an exception:

[...] PgEnum cannot be cast to java.lang.Enum

Hopefully I'm just doing something wrong, but it seems buried pretty deep in the DAO code.
I can provide more of my code, and more stack trace if it helps :)

My code in question looks like:

object ChallengeTable : IntIdTable() {
  val createdAt = datetime("createdAt")
  val slug = varchar("slug", 256).uniqueIndex()
  val status = ChallengeStatus.pgColumn(this, "status")
  val entryId = varchar("entryId", 64)
}

class ExposedChallenge(id: EntityID<Int>) : IntEntity(id) {
  companion object : IntEntityClass<ExposedChallenge>(ChallengeTable)

  var createdAt by ChallengeTable.createdAt
  var slug by ChallengeTable.slug
  var status by ChallengeTable.status
  var entryId by ChallengeTable.entryId
}

enum class ChallengeStatus {
  draft,
  live,
  completed,
  archive;

  companion object {
    const val dbName = "challenge_status"

    fun pgColumn(table: Table, name: String) = table.customEnumeration(
      name = name,
      sql = dbName,
      fromDb = { it.fromPg() },
      toDb = { it.toPg() }
    )

    private fun Any.fromPg() = valueOf(this as String)
    private fun ChallengeStatus?.toPg() = PgEnum(this)
  }

  class PgEnum(enumValue: ChallengeStatus?) : PGobject() {
    init {
      value = enumValue?.name
      type = dbName
    }
  }
}

And the call site (simplified):

transaction {
  ExposedChallenge
    .find { (ChallengeTable.slug eq "exampleSlug") }
    .first().apply {
      status = challengeStatus
    }
}

The first few stack slices:

java.lang.ClassCastException: ChallengeStatus$PgEnum cannot be cast to java.lang.Enum
        at org.jetbrains.exposed.sql.Table$customEnumeration$1.notNullValueToDB(Table.kt:235)
        at org.jetbrains.exposed.sql.IColumnType$DefaultImpls.nonNullValueToString(ColumnType.kt:51)
        at org.jetbrains.exposed.sql.ColumnType.nonNullValueToString(ColumnType.kt:60)
        at org.jetbrains.exposed.sql.IColumnType$DefaultImpls.valueToString(ColumnType.kt:43)
        at org.jetbrains.exposed.sql.ColumnType.valueToString(ColumnType.kt:60)
        at org.jetbrains.exposed.sql.QueryBuilder$registerArguments$1.invoke(Expression.kt:19)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions