【问题标题】:Schema-validation: missing table [...] error message using flyway for a SQL code generated by HibernateSchema-validation:missing table [...] 错误消息使用 flyway 用于 Hibernate 生成的 SQL 代码
【发布时间】:2019-06-18 23:58:15
【问题描述】:

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

@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 会为每个类创建一个外键,但更大的问题是我仍然收到错误消息

 Schema-validation: missing table [custom_table]

我尝试将 custom_table 重命名为 customtable(并在 Java 中重命名该类),但错误消息相同。

你遇到过同样的问题吗?你有什么建议吗?我已经解决这个问题了 - 至少 - 2 天。

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

谢谢。

【问题讨论】:

  • 尝试在您的类中添加@Table 注释,并在其中提供表名。
  • 我试过了。不幸的是,情况并没有改变

标签: java spring hibernate gradle flyway


【解决方案1】:

最后我得到了问题。问题在于插入多个外键。 (所以这两行):

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 的表重新创建整个结构时,在整个项目中执行完全相同的操作,成功了。

【讨论】:

    猜你喜欢
    • 2020-07-16
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 2011-12-12
    相关资源
    最近更新 更多