在对自定义Postgres的“对象类型”执行WHERE子句操作时,我得到了以下PSQLException。
异常
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.我已经跟踪了Ktorm官方指南,但是没有提到定制的Postgres类型。如有任何指示/帮助,将不胜感激。请参阅下面的代码以复制:
谢谢。
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 }
}发布于 2020-12-16 17:04:11
在向Ktorm的维护人员寻求帮助之后,新版本的ktorm支持本地Postgressql enum对象类型。在我的例子中,我需要pgEnum,而不是默认的ktrom enum函数,该函数将枚举转换为varchar,从而导致Postressql中类型的冲突:
但是,在编写ktorm时注意,ktorm的pgEnum函数仅在ktormv3.2.x +中。Jcentral和mavenCentral maven存储库中的最新版本是v3.1.0。这是因为在最新版本中,还有一个组名从me.liuwj.ktorm更改为org.ktorm。因此,升级还意味着将依赖项中的组名更改为新的组名,以匹配maven repos。这是对我的项目的无缝升级,新的pgEnum在我的用例中起了作用。
对于上面的代码示例来说,这意味着交换
object SuppliersInstanceTable : Table<SuppliersInstance>("instance") {
val rate = enum("rate", typeRef<Rate>()).primaryKey().bindTo { it.rate } <---
...
}为
object SuppliersInstanceTable : Table<SuppliersInstance>("instance") {
val rate = pgEnum<Rate>("rate").primaryKey().bindTo { it.rate } <---
...
}https://stackoverflow.com/questions/65241095
复制相似问题