首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Kotlin的Ktorm对自定义Postgres的“对象类型”执行WHERE子句操作以避免"PSQLException: ERROR: operator不存在“

如何使用Kotlin的Ktorm对自定义Postgres的“对象类型”执行WHERE子句操作以避免"PSQLException: ERROR: operator不存在“
EN

Stack Overflow用户
提问于 2020-12-10 19:30:33
回答 1查看 631关注 0票数 0

在对自定义Postgres的“对象类型”执行WHERE子句操作时,我得到了以下PSQLException。

异常

代码语言:javascript
复制
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类型。如有任何指示/帮助,将不胜感激。请参阅下面的代码以复制:

谢谢。

  1. 将产生上述异常的示例测试
代码语言:javascript
复制
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. 模式
代码语言:javascript
复制
-- 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的Ktorms实体与绑定
代码语言:javascript
复制
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 }
}
EN

回答 1

Stack Overflow用户

发布于 2020-12-16 17:04:11

在向Ktorm的维护人员寻求帮助之后,新版本的ktorm支持本地Postgressql enum对象类型。在我的例子中,我需要pgEnum,而不是默认的ktrom enum函数,该函数将枚举转换为varchar,从而导致Postressql中类型的冲突:

pgEnum参考资料

但是,在编写ktorm时注意,ktorm的pgEnum函数仅在ktormv3.2.x +中。JcentralmavenCentral maven存储库中的最新版本是v3.1.0。这是因为在最新版本中,还有一个组名从me.liuwj.ktorm更改为org.ktorm。因此,升级还意味着将依赖项中的组名更改为新的组名,以匹配maven repos。这是对我的项目的无缝升级,新的pgEnum在我的用例中起了作用。

对于上面的代码示例来说,这意味着交换

代码语言:javascript
复制
object SuppliersInstanceTable : Table<SuppliersInstance>("instance") {
    val rate = enum("rate", typeRef<Rate>()).primaryKey().bindTo { it.rate } <---
    ...
}

代码语言:javascript
复制
object SuppliersInstanceTable : Table<SuppliersInstance>("instance") {
    val rate = pgEnum<Rate>("rate").primaryKey().bindTo { it.rate } <---
    ...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65241095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档