How to use Ktorm to perform WHERE clause operation on custom Postgres “object type” avoiding “PSQLException: ERROR: operator does not exist” #216
-
Whilst performing a WHERE clause operation on a custom Postgres "object type" I ended up the following PSQLException. Exception
I have followed the Ktorm official guides here but, there is no mention of custom Postgres types. Any pointers to where I'm being wrong would be highly appreciated. See code below to reproduce: Thank you.
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)
}
}
}
-- 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;
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 }
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Converted to an issue. Please close/delete this |
Beta Was this translation helpful? Give feedback.
-
The default object SuppliersInstanceTable : Table<SuppliersInstance>("instance") {
val rate = pgEnum<Rate>("rate").primaryKey().bindTo { it.rate }
val instanceSeq = int("instance_value").primaryKey().bindTo { it.instanceSeq }
} |
Beta Was this translation helpful? Give feedback.
The default
enum
function in the core module just converts enum values as strings. You should usepgEnum
instead. https://github.com/kotlin-orm/ktorm/blob/master/ktorm-support-postgresql/src/main/kotlin/org/ktorm/support/postgresql/SqlTypes.kt#L84-L110