如何修复不兼容的外键约束spring boot

Posted

技术标签:

【中文标题】如何修复不兼容的外键约束spring boot【英文标题】:how to fix incompatible foreign key constraints spring boot 【发布时间】:2021-08-19 03:50:09 【问题描述】:

我有一个 Spring Boot 应用程序,其中有两个实体。会议设置和会议时间。一个会议设置可以有多个会议时间,一个会议时间属于一个会议设置。

我通过 sql 脚本生成数据库并为 meetingTime 实体引用它们的外键。但我收到以下错误:

Caused by: java.sql.SQLException: Referencing column 'meeting_name' and referenced column 'id' in foreign key constraint 'FK1omm6fk51xdsd0kysqbmleweg' are incompatible.

有趣的是,我没有在我的实体和脚本中的任何地方引用 id。这是我的实体和脚本的样子:

会议设置:

@Entity
@Table(name = "meeting_settings")
@Data

public class MeetingsSetting 

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "meeting_name")
    private String meetingName;

    @Column(name = "meeting_url")
    private String meetingUrl;

    @Column(name = "meeting_pw")
    private String meetingPw;

    @OneToMany(mappedBy = "meeting_Name", cascade = CascadeType.ALL)
    private Set<MeetingTime> meetingTime = new HashSet<>();

会议时间:

@Entity
@Table(name = "meeting_times")
@Data
public class MeetingTime 

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "meeting_date")
    private String date;

    @Column(name = "start_time")
    private String startTime;

    @Column(name = "end_time")
    private String endTime;

    @Column(name = "meeting_name",insertable = false, updatable = false )
    private String meetingName;

    @ManyToOne
    @JoinColumn(name = "meeting_name", nullable = false)
    private MeetingsSetting meeting_Name;

这是我生成表格的脚本:

-- auto-generated definition
create table meeting_settings
(
    id           bigint auto_increment
        primary key,
    meeting_name varchar(255) null,
    meeting_pw   varchar(255) null,
    meeting_url  varchar(255) null
);

create table meeting_times
(
    id                  bigint auto_increment
        primary key,
    meeting_date        varchar(255) null,
    start_time          varchar(255) null,
    end_time            varchar(255) null,
    meeting_name        varchar(255) null,
    constraint fk_meeting_times_meeting_name
        foreign key (meeting_name) references meeting_settings (meeting_name)
);

什么可能导致这样的错误?因为我没有在任何地方引用 id。

更新:

spring.datasource.url=jdbc:mysql://localhost:3306/coorporate_blinddate?createDatabaseIfNotExist=true&useSSL=true&serverTimezone=UTC
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=../generate.sql
spring.jpa.show-sql= true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=Test1234@@1&
server.port=8081

休眠序列:

Hibernate: create table meeting_settings (id bigint not null auto_increment, meeting_name varchar(255), meeting_pw varchar(255), meeting_url varchar(255), primary key (id)) engine=InnoDB
Hibernate: create table meeting_settings_meeting_time (meetings_setting_id bigint not null, meeting_name bigint not null, primary key (meetings_setting_id, meeting_name)) engine=InnoDB
Hibernate: create table meeting_times (id bigint not null auto_increment, meeting_date varchar(255), end_time varchar(255), meeting_name varchar(255), start_time varchar(255), primary key (id)) engine=InnoDB
Hibernate: alter table meeting_settings drop index UK_klg4vqmhi7o9qff83ymly598o
Hibernate: alter table meeting_settings add constraint UK_klg4vqmhi7o9qff83ymly598o unique (meeting_name)
Hibernate: alter table meeting_settings_meeting_time drop index UK_jsn83wsxfkpm1xfencvsdkqj1
Hibernate: alter table meeting_settings_meeting_time add constraint UK_jsn83wsxfkpm1xfencvsdkqj1 unique (meeting_name)
Hibernate: alter table meeting_settings_meeting_time add constraint FK9lq62drkkslq6x381b3lieruu foreign key (meeting_name) references meeting_times (id)
Hibernate: alter table meeting_settings_meeting_time add constraint FKglhgb5vgsviqm7t6vtmdx5e7t foreign key (meetings_setting_id) references meeting_settings (id)
Hibernate: alter table meeting_times add constraint FK1omm6fk51xdsd0kysqbmleweg foreign key (meeting_name) references meeting_settings (id)

生成的表格

meeting_settings_meeting_time
meetings_setting_id
meeting_name
PRIMARY
UK_jsn83wsxfkpm1xfencvsdkqj1
FK9lq62drkkslq6x381b3lieruu
FKglhgb5vgsviqm7t6vtmdx5e7t
UK_jsn83wsxfkpm1xfencvsdkqj1

【问题讨论】:

【参考方案1】:

看起来您的 meeting_name 列不是唯一的,以便建立您已完成的关系。添加unique = true

@Column(name = "meeting_name", unique = true)
private String meetingName;

@OneToMany(cascade = CascadeType.ALL)
@JoinTable(inverseJoinColumns=@JoinColumn(name="meeting_name"))
private Set<MeetingTime> meetingTime = new HashSet<>();

【讨论】:

您好,很抱歉回复晚了。我在 meetingSettings 和 meetingTime 中尝试过,但仍然出现相同的错误。使用 ID 参考 @haso 我更新了我的答案,你可以 JoinTable 说它是我的加入专栏。 感谢您的回答,但现在又出现了另一个错误。我想我要疯了哈哈。标记为 mappedBy 的关联不得定义 JoinTable 或 JoinColumn 等数据库映射:com.cbc.coorporateblinddateservice.entities.meetinsSetting.MeetingsSetting.meetingTime 如果我取出 mappedBy 部分,旧错误又来了...with id 我在日志中找到了这两个:Error execution DDL "alter table meeting_times drop foreign key FK1omm6fk51xdsd0kysqbmleweg" via JDBC Statement Table 'coorporate_blinddate.meeting_settings_meeting_time' does not exist 我总是在开始之前删除表应用。这是错误吗?

以上是关于如何修复不兼容的外键约束spring boot的主要内容,如果未能解决你的问题,请参考以下文章

Oracle如何创建一个置空的外键约束?

spring boot jpa中的多对一映射中的外键未在子表中更新

如何修复 MySQL 中的外键错误?

修复 ORA-02273: 这个唯一/主键被一些外键引用

AWS RDS 的外键约束不起作用

oracle为表建立外键时没有命名FOREIGN KEY约束,现在想删外键怎么办??