【问题标题】:Upsert with "on conflict do update" with partial index in jOOQ在 jOOQ 中使用带有部分索引的“冲突更新”进行更新
【发布时间】:2021-07-21 07:50:53
【问题描述】:

我在表 MY_TABLE 上有一个部分索引,其定义如下:

CREATE UNIQUE INDEX my_index 
    ON MY_TABLE(CUSTOMER_ID, OWNER_ID) 
    WHERE CLOSED_ON IS NULL;

然后,我正在尝试使用 jOOQ 进行更新插入

final var insert = using(configuration)
           .insertInto(MY_TABLE,
                       MY_TABLE.CUSTOMER_ID,
                       MY_TABLE.OWNER_ID,
                       MY_TABLE.UPDATED_ON);
customers.forEach(custId -> insert.values(custId, reprId, today));
insert.onConflict(MY_TABLE.CUSTOMER_ID, MY_TABLE.OWNER_ID)
    .where(MY_TABLE.CLOSED_ON.isNull())
    .doUpdate()
    .set(MY_TABLE.UPDATED_ON, today)
    .execute();

但是上面的代码抛出了这个错误:

org.jooq.exception.DataAccessException: SQL [...];
   ERROR: invalid reference to FROM-clause entry for table "my_table"
   Hint: There is an entry for table "my_table", but it cannot be referenced from this part of the query.

查看生成的 SQL,我看到 jOOQ 正在将架构和表名添加到 on conflictwhere 部分,这是 Postgres 不喜欢的:

... on conflict ("customer_id", "owner_id") where "my_schema"."my_table"."closed_on" is null do update set ...

有没有办法告诉 jOOQ 不要添加模式和表名?

这是我正在使用的解决方法,但我想知道是否有更好的方法:

.where(condition("closed_on IS NULL"))

【问题讨论】:

    标签: postgresql jooq


    【解决方案1】:

    我们在 jOOQ 的问题页面中发现了一个错误报告,该报告正好提出了这个问题:

    https://github.com/jOOQ/jOOQ/issues/11732

    其中提到了这种解决方法:

    .where(field(COURSE_ENROLL.DELETED_TS.getUnqualifiedName()).isNull())
    

    所以我可以这样做,而不是在我的例子中使用文字字符串。

    这也被提到是 3 周前修复的错误 :)

    谢谢。

    编辑: jOOQ 3.14.9 中已经对此进行了修复

    【讨论】:

    • 谢谢,最终确实使用了 3.14.9
    猜你喜欢
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    相关资源
    最近更新 更多