简体   繁体   English

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

[英]How to make jOOQ generate composite foreign keys not referencing a primary key?

Using the latest jOOQ with H2 1.4.194 no foreign keys in Keys.java are generated for the following (reduced) schema:使用与H2在1.4.194没有外键的最新jOOQ 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));

Adding a unique constraint as follows doesn't help:如下添加唯一约束无济于事:

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));

Changing the FK to reference the primary key (whether simple or composite) makes the FK appear.更改 FK 以引用主键(无论是简单键还是复合键)都会使 FK 出现。

Querying the FK via H2 works as expected.通过 H2 查询 FK 按预期工作。

Doesn't look like a regression (tried various 3.x jOOQ's), so wondering if I'm doing something wrong.看起来不像回归(尝试了各种 3.x jOOQ),所以想知道我是否做错了什么。 Generator config (via maven plugin) as follows:生成器配置(通过 maven 插件)如下:

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

Working around missing foreign keys解决缺少外键的问题

Starting from jOOQ 3.14, if you're missing foreign keys from generated output either because of a limitation in jOOQ, or because you don't actually have any foreign keys in your schema, or because you're generating views, you can create synthetic objects , including synthetic primary keys , synthetic unique keys , and reference them from synthetic foreign keys with a configuration like this:从 jOOQ 3.14 开始,如果由于 jOOQ 中的限制,或者因为您的架构中实际上没有任何外键,或者因为您正在生成视图,您在生成的输出中缺少外键,您可以创建合成对象,包括合成主键合成唯一键,并从合成外键中引用它们,配置如下:

<configuration>
  <generator>
    <database>
      <syntheticObjects>
        <foreignKeys>
          <foreignKey>
            <tables>U</tables>
            <fields>
              <field>A</field>
              <field>B</field>
            </fields>
            <referencedTable>T</referencedTable>
            <referencedFields>
              <field>A</field>
              <field>B</field>
            </referencedFields>
          </foreignKey>
        </foreignKeys>
      </syntheticObjects>
    </database>
  </generator>
</configuration>

There are different ways to configure this, eg by name reference or by column reference, etc. check out the manual for details .有不同的配置方法,例如通过名称引用或通过列引用等。 查看手册了解详细信息

Historic answer (it was a bug)历史答案(这是一个错误)

The query that is run internally by jOOQ's code generator is this: 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

The query appears correct, but there seems to be a misunderstanding in how H2 encodes unique constraints in these dictionary views.查询看起来是正确的,但似乎对 H2 如何在这些字典视图中编码唯一约束存在误解。 Or a bug in H2.或者 H2 中的错误。

I've created two issues, let's see which one is right:我创建了两个问题,让我们看看哪个是正确的:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM