当前位置: 代码迷 >> 综合 >> Hibernate报错 Cannot add foreign key constraint
  详细解决方案

Hibernate报错 Cannot add foreign key constraint

热度:73   发布时间:2023-09-27 02:22:02.0

GenerationTarget encountered exception accepting command : Error executing DDL “alter table picture_info add constraint FK2ff6f2f6dg6av1beayxujnunm foreign key (base64file_id) references base64file (id)” via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL “alter table picture_info add constraint FK2ff6f2f6dg6av1beayxujnunm foreign key (base64file_id) references base64file (id)” via JDBC Statement
Caused by: java.sql.SQLException: Cannot add foreign key constraint

原因:
添加了一个一对一的表格外键如下:

@Entity
@ApiModel(description = "图片详情")
public class PictureInfo extends BasePerson {private boolean enabled = false;private Base64File base64File;@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)@ApiModelProperty("Base64文件对象")public Base64File getBase64File() {return base64File;}

解决办法

https://blog.csdn.net/h1101723183/article/details/78400768
根据这个帖子里面的可行
添加一行

@JoinColumn(name = "base64file_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))//修复添加外键失败的bug但是不知道有没有副作用

加在public Base64File getBase64File()上面就可以了.暂时不知道有啥副作用
加了就不会报错了

  相关解决方案