ktorm icon indicating copy to clipboard operation
ktorm copied to clipboard

How to use Ktorm to perform WHERE clause operation on custom Postgres “object type” avoiding “PSQLException: ERROR: operator does not exist

Open DaddyMoe opened this issue 5 years ago • 1 comments

Whilst performing a WHERE clause operation on a custom Postgres "object type" I ended up the following PSQLException.

Exception

org.postgresql.util.PSQLException: ERROR: operator does not exist: rate = character varying
  Hint: No operator matches the given name and argument types. You might need to add explicit type casts.

I have followed the Ktorm official guides here but, there is no mention of custom Postgres object types. See code below to reproduce:

Thank you.

  1. Example test that would produce the above exception
internal class SuppliersInstanceDAOTest {


    @Test
    fun shouldReturnInstanceSequence() {

        val database = Database.connect("jdbc:postgresql://localhost:5432/mydb", user = "postgres", password = "superpassword")

        val instanceDate: LocalDate = LocalDate.of(2019, 4, 1)
        database.withSchemaTransaction("suppliers") {
            database.from(SuppliersInstanceTable)
                .select(SuppliersInstanceTable.instanceSeq)
                .whereWithConditions {
                // The following line causes "ERROR: operator does not exist: rate = character varying"
                    it += SuppliersInstanceTable.rate eq Rate.DAILY
                }.asIterable()
                .first()
                .getInt(1)
        }
        
    }
}
  1. Schema
-- Note the special custom enum object type here that I cannot do anything about
CREATE TYPE suppliers.rate AS ENUM
    ('Daily', 'Byweekly');

CREATE TABLE suppliers.instance
(
    rate suppliers.rate NOT NULL,
    instance_value integer NOT NULL
)

TABLESPACE pg_default;

  1. Kotlin's Ktorms Entities and bindings

enum class Rate(val value: String) {
    DAILY("Daily"),
    BIWEEKLY("Byweekly")
}

interface SuppliersInstance : Entity<SuppliersInstance> {
    companion object : Entity.Factory<SuppliersInstance>()
    val rate: Rate
    val instanceSeq: Int
}

object SuppliersInstanceTable : Table<SuppliersInstance>("instance") {
    val rate = enum("rate", typeRef<Rate>()).primaryKey().bindTo { it.rate } // <-- Suspect
    //val rate = enum<Rate>("rate", typeRef()).primaryKey().bindTo { it.rate } // Failed too

    val instanceSeq = int("instance_value").primaryKey().bindTo { it.instanceSeq }
}

DaddyMoe avatar Dec 11 '20 13:12 DaddyMoe

Use pgEnum instead of enum. https://github.com/kotlin-orm/ktorm/discussions/216#discussioncomment-198775

vincentlauvlwj avatar Dec 12 '20 07:12 vincentlauvlwj