I have added some Postgresql types to Exposed as extensions. It has two ready types named enumeration
and enumerationByName
. I tested both of them with no success for mapping a postgre enum type to Kotlin enum class. In both reading and writing it raises error
enum class TicketStatus(val status: String) {
Open("open"),
Close("close"),
InProgress("in_progress")
}
class Ticket(id: EntityID<UUID>) : Entity<UUID>(id) {
companion object : EntityClass<UUID, Ticket>(Tickets)
var geom by Tickets.geom
var description by Tickets.description
var status by Tickets.status
var createdAt by Tickets.createdAt
var updatedAt by Tickets.updatedAt
var owner by Tickets.owner
}
When reading:
java.lang.IllegalStateException: open is not valid for enum TicketStatus
Enumerated types can be selected in the same way as standard data types. You can pick the Enum type from the datatype drop-down. Tip: A default value can be defined for the column. Expand the column detail by clicking the arrow icon and specify the value in the Default value field.
Since Kotlin enums are classes, they can have their own properties, methods, and implement interfaces.
Enumerated (enum) types are data types that comprise a static, ordered set of values. They are equivalent to the enum types supported in a number of programming languages. An example of an enum type might be the days of the week, or a set of status values for a piece of data.
Enums can be used in kotlin's when construct, We must use the else block to handle if we receive any value other Than value present in the Enum.
You should declare status column as follow:
object Tickets: Table() {
val status = enumeration("status", TicketStatus::class.java) // will create integer column
val status = enumerationByName("status", TicketStatus::class.java) // will create varchar with TicketStatus names
}
I realize this is an old question, but if anyone is still looking for an answer here you go:
Exposed has a customEnumeration
that can be used to deal with Enums, and is particularly useful if you are using Strings, or another non-default enumerator to back your enum.
For postgres, you'll first need to define a class like so:
class PGEnum<T:Enum<T>>(enumTypeName: String, enumValue: T?) : PGobject() {
init {
value = enumValue?.name
type = enumTypeName
}
}
Then in your table definition, use the following to define your column, replacing the placeholders as necessary:
val enumColumn = customEnumeration(
"ENUM_COLUMN",
"ENUM_SCHEMA.ENUM_TYPE",
{ value ->
when (value) {
is PGobject -> LocalEnumClass.valueOf(value.value)
is String -> LocalEnumClass.valueOf(value)
else -> error("Can't convert ENUM_COLUMN")
}
},
{ PGEnum("ENUM_SCHEMA.ENUM_TYPE", it) }
)
I was running into the same org.postgresql.util.PGobject is not valid for enum
before I did this.
See here for more information, and for non-Postgres databases
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With