Rodrigo Silva
06/18/2020, 4:07 PMRodrigo Silva
06/18/2020, 4:08 PMdata class Person(
// others attributes
val maritalStatus: MaritalStatus,
val gender: Gender
) : BaseEntity()
abstract class PersonBaseTable(schema: String) : Table("$schema.person") {
// others attributes
val maritalStatus = enumerationByName("marital_status", 15, MaritalStatus::class)
val gender = enumerationByName("gender", 15, Gender::class)
override val primaryKey: PrimaryKey = PrimaryKey(id, name = "person_id_uindex")
}
fun personTable(schema: String): PersonBaseTable = object : PersonBaseTable(schema) {}
and:
enum class MaritalStatus {
Married,
Single,
Divorced,
StableUnion,
Widower,
Uninformed
}
enum class Gender {
Male,
Female,
Uninformed
}
my sql file (I'm using postgresql)
CREATE TYPE marital_status_enum AS ENUM ( 'Married','Single','Divorced','StableUnion','Widower','Uninformed');
CREATE TYPE gender_enum AS ENUM ( 'Male','Female','Uninformed');
CREATE TABLE IF NOT EXISTS rjdesenvolvimento.person
(
//others columns
marital_status marital_status_enum not null,
gender gender_enum
);
and:
entity.person?.let {
personTable(schema).insert { personTable ->
personTable[maritalStatus] = it.maritalStatus
personTable[gender] = it.gender
}
But I am getting the following error.
org.jetbrains.exposed.exceptions.ExposedSQLException: org.postgresql.util.PSQLException: ERROR: column "marital_status" is of type marital_status_enum but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Ivan
06/18/2020, 4:14 PMclass PGEnum<T : Enum<T>>(enumTypeName: String, enumValue: T?) : PGobject() {
init {
value = enumValue?.name
type = enumTypeName
}
}
object EnumTable : Table() {
val enumColumn = customEnumeration("enumColumn", "FooEnum", {value -> Foo.valueOf(value as String)}, { PGEnum("FooEnum", it) }
}
...
transaction {
exec("CREATE TYPE FooEnum AS ENUM ('Bar', 'Baz');")
SchemaUtils.create(EnumTable)
...
}
Rodrigo Silva
06/18/2020, 8:06 PM