架构验证:缺少 table [...] 错误消息,使用 flyway 作为 Hibernate 生成的 SQL 代码

Schema-validation: missing table [...] error message using flyway for a SQL code generated by Hibernate

我一直在尝试处理一些关于 Flyway 的问题。我的情况如下:我有两个 Java classes,我想将它们作为两个模式迁移。我们将它们命名为 Table 和 CustomTable。我的 java class 看起来像:

@Entity
public class xtable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
//getters, setters, constructors

@Entity
public class CustomTable{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String a;
private String b;
private String c;
//getters, setters, constructors

我的application.properties:

spring.flyway.url=${env.var1}
spring.flyway.user=${env.var2}
spring.flyway.password=${env.var3}
spring.jpa.hibernate.ddl-auto=validate

//If I use create-drop, hibernate creates it, but after that the validation fails

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
spring.logging.level.org.hibernate.SQL=debug
spring.jpa.show-sql=true
hibernate.temp.use_jdbc_metadata_defaults=true
spring.flyway.enabled=true

我的build.gradle:

plugins {
    id "org.flywaydb.flyway" version "5.2.4"
}

dependencies {
    implementation 'org.flywaydb:flyway-core'
}

情况很奇怪,因为它甚至不能使用自动生成的 SQL 代码,我让程序在没有 flyway 的情况下创建它。

看起来像这样:

    create table custom_table (
  id         bigint not null,
  a          varchar(255),
  b          varchar(255),
  c          varchar(255),
  xtable_id bigint,
  primary key (id)
)
  engine = InnoDB;

create table xtable (
  id                  bigint not null,
  name                varchar(255),
  xtable_id bigint,
  primary key (id)
)
  engine = InnoDB;

alter table custom_table
  add constraint FKep6vooglihwraille12muox9 foreign key (xtable_id) references xtable (id);

alter table xtable
  add constraint FK426q765pr4gv5wux6jaktafqk foreign key (custom_table_id) references custom_table (id);

我也不明白为什么 Hibernate 会在每个 class 中创建一对一的外键,但更大的问题是我仍然收到错误消息

 Schema-validation: missing table [custom_table]

我尝试将 custom_table 重命名为 customtable(并重命名 Java 中的 class),但错误消息是一样的。

你遇到过同样的问题吗?你有什么建议吗?我一直在研究这个问题 - 至少 - 2 天。

我在这里寻找相关的或看似相同的主题,但找不到好的解决方案。

谢谢。

终于找到问题了。问题在于插入多个外键。 (所以这两行):

alter table custom_table
add constraint FKep6vooglihwraille12muox9 foreign key (xtable_id) references xtable (id);

alter table xtable
  add constraint FK426q765pr4gv5wux6jaktafqk foreign key (custom_table_id) references custom_table (id);

不过,我无法弄清楚 Flyway 无法处理这个问题的原因,但是当我用两个表和另一个包含正确 ID 的表重新创建整个结构时,在整个项目,它的工作。