jOOQ:在特定 table 中查找约束

jOOQ: Find constraints in a specific table

在与 jOOQ 无关(没有代码生成)的迁移中工作时,我遇到了一种情况,我需要检查数据库中是否已经存在约束(唯一的外键)以完成进一步的操作。

到目前为止我尝试的是 运行 删除并尝试捕获异常,但它失败了事务并停止了后续迁移的发生

dsl.alterTable(table).dropConstraint(constraintName).execute();
...
>>  ERROR: constraint "t_client_name_unique" of relation "t_client" does not exist

设置:

未来的解决方案(jOOQ 3.12 中不可用)

对于 jOOQ 3.13+,我们正在大力投资以支持更多此类迁移方案。将来,我们将支持一些与供应商无关的 information_schema 样式视图,这些视图可为所有数据库生成此类元信息:#8301

另一个可以立即为您提供帮助的功能是本机 DROP CONSTRAINT IF EXISTS 支持:#9557. You could, of course, use plain SQL to run this particular statement on PostgreSQL, until #9557 可用

现在的解决方案

或者,在您的情况下,由于您只使用 PostgreSQL,您可以通过查询 PostgreSQL 的 information_schema 直接执行此操作。您可以生成 information_schema 个表,然后 运行 这个查询:

select *
from information_schema.table_constraints
where constraint_schema = :constraint_schema
and constraint_name = :constraint_name