How to get correct type and nullability information for enum fields using jOOQ's metadata API?

Viewed 19

I'm trying to use jOOQ's metadata API, and most columns behave the way I'd expect, but enum columns seem to be missing type and nullability information somehow.

For example, if I have a schema defined as:

CREATE TYPE public.my_enum AS ENUM (
    'foo',
    'bar',
    'baz'
);

CREATE TABLE public.my_table (
    id bigint NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    name text,
    my_enum_column public.my_enum NOT NULL,
);

The following test passes:

// this is Kotlin, but hopefully pretty easy to decipher
test("something fishy going on here") {
    val jooq = DSL.using(myDataSource, SQLDialect.POSTGRES)
    val myTable = jooq.meta().tables.find { it.name == "my_table" }!!

    // This looks right...
    val createdAt = myTable.field("created_at")!!
    createdAt.dataType.nullability() shouldBe Nullability.NOT_NULL
    createdAt.dataType.typeName shouldBe "timestamp with time zone"

    // ...but none of this seems right
    val myEnumField = myTable.field("my_enum_column")!!
    myEnumField.dataType.typeName shouldBe "other"
    myEnumField.dataType.nullability() shouldBe Nullability.DEFAULT
    myEnumField.dataType.castTypeName shouldBe "other"
    myEnumField.type shouldBe Any::class.java
}
  1. It's telling me that enum columns have Nullability.DEFAULT regardless of whether they are null or not null. For other types, Field.dataType.nullability will vary depending on whether the column is null or not null, as expected.

  2. For any enum column, the type is Object (Any in Kotlin), and the dataType.typeName is "other". For non-enum columns, dataType.typeName gives me the correct SQL for the type.

I'm also using the jOOQ code generator, and it generates the correct types for enum columns. That is, it creates an enum class and uses that as the type for the corresponding fields. So I think jOOQ has the type information, but how can I access it via the metadata API?

I'm using postgres:11-alpine and org.jooq:jooq:3.14.11.

0 Answers
Related