Hello everyone. I have a little problem.
# exposed
r
Hello everyone. I have a little problem.
I have the following class:
Copy code
data 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:
Copy code
enum class MaritalStatus {
  Married,
  Single,
  Divorced,
  StableUnion,
  Widower,
  Uninformed
}

enum class Gender {
  Male,
  Female,
  Uninformed
}
my sql file (I'm using postgresql)
Copy code
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:
Copy code
entity.person?.let {
  personTable(schema).insert { personTable ->
    personTable[maritalStatus] = it.maritalStatus
    personTable[gender] = it.gender
  }
But I am getting the following error.
Copy code
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.
i
I Never use enum with postgres but on wiki link you can find this example
Copy code
class PGEnum<T : Enum<T>>(enumTypeName: String, enumValue: T?) : PGobject() {
    init {
        value = enumValue?.name
        type = enumTypeName
    }
}
Copy code
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)
   ...
}
r
Thank you @Ivan