如何让jOOQ生成不引用主键的复合外键?

时间:2017-03-14 22:56:19

标签: java h2 jooq

使用最新的jOOQ和H2 1.4.194,Keys.java中的外键不会为以下(简化)架构生成:

CREATE TABLE t (a INT, b INT, PRIMARY KEY (a));
CREATE TABLE u (a INT, b INT, FOREIGN KEY (a,b) REFERENCES t (a,b));

如下添加唯一约束无济于事:

CREATE TABLE t (a INT, b INT, PRIMARY KEY (a), UNIQUE (a,b));
CREATE TABLE u (a INT, b INT, FOREIGN KEY (a,b) REFERENCES t (a,b));

更改FK以引用主键(无论是简单还是复合)会使FK出现。

通过H2查询FK按预期工作。

看起来不像回归(试过各种各样的3.x jOOQ),所以想知道我做错了什么。 Generator配置(通过maven插件)如下:

<name>org.jooq.util.JavaGenerator</name>
<database>
    <name>org.jooq.util.h2.H2Database</name>
    <includes>.*</includes>
    <excludes />
    <inputSchema>PUBLIC</inputSchema>
</database>

1 个答案:

答案 0 :(得分:1)

jOOQ代码生成器内部运行的查询是:

select 
  "CROSS_REFERENCES"."FK_NAME", 
  "CROSS_REFERENCES"."FKTABLE_NAME", 
  "CROSS_REFERENCES"."FKTABLE_SCHEMA", 
  "CROSS_REFERENCES"."FKCOLUMN_NAME", 
  "CONSTRAINTS"."CONSTRAINT_NAME", 
  "CONSTRAINTS"."CONSTRAINT_SCHEMA"
from "INFORMATION_SCHEMA"."CROSS_REFERENCES"
  join "INFORMATION_SCHEMA"."CONSTRAINTS"
  on (
    "CROSS_REFERENCES"."PK_NAME" = "CONSTRAINTS"."UNIQUE_INDEX_NAME"
    and "CROSS_REFERENCES"."PKTABLE_NAME" = "CONSTRAINTS"."TABLE_NAME"
    and "CROSS_REFERENCES"."PKTABLE_SCHEMA" = "CONSTRAINTS"."TABLE_SCHEMA"
  )
where (
  "CROSS_REFERENCES"."FKTABLE_SCHEMA" in (
    'PUBLIC'
  )
  and "CONSTRAINTS"."CONSTRAINT_TYPE" in (
    'PRIMARY KEY', 'UNIQUE'
  )
)
order by 
  "CROSS_REFERENCES"."FKTABLE_SCHEMA" asc, 
  "CROSS_REFERENCES"."FK_NAME" asc, 
  "CROSS_REFERENCES"."ORDINAL_POSITION" asc

查询显示正确,但似乎对H2如何编码这些字典视图中的唯一约束存在误解。或H2中的错误。

我创建了两个问题,让我们看看哪个是正确的: