简体   繁体   English

我无法删除记录“无法删除或更新父行:外键约束失败。我该如何解决这个问题?

[英]I cannot delete a record "Cannot delete or update a parent row: a foreign key constraint fails. How do I fix this?

I tried to delete a record in ratings table,then我试图删除评级表中的记录,然后

Cannot add or update a child row: a foreign key constraint fails ( fyprojectdb . ratings , CONSTRAINT FKdyash6f91887unaan9mj9b460 FOREIGN KEY ( answer_id ) REFERENCES answers ( answer_id ))无法添加或更新子行:外键约束失败( fyprojectdb . ratings , CONSTRAINT FKdyash6f91887unaan9mj9b460 FOREIGN KEY ( answer_id ) REFERENCES answers ( answer_id ))

this error occured.发生此错误。 How to fix this error.如何修复此错误。 I have mapped both entities correctly.我已经正确映射了两个实体。

@Entity
@Table(name = "ratings")
public class Ratings {

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

    @Column(nullable = false, unique = false, length = 45)
    private Short ratingValue;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "userId")
    private User user;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "answer_id")
    private Answer answer;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "question_id")
    private Question question;
//getters and setters


@Entity
@Table(name = "answers")
public class Answer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long answer_id;

    @Column(nullable = false, unique = false, length = 100)
    private String fullAnswer;

    /*Many to one mapping question*/
    @ManyToOne(cascade = CascadeType.REMOVE)
    @JoinColumn(name = "question_id")
    private Question question;

    /* Many to One mapping with users*/
    @ManyToOne(cascade = CascadeType.REMOVE)
    @JoinColumn(name = "userId")
    private User user;
//getters and setters

The reason is cascade = CascadeType.ALL here原因是这里的cascade = CascadeType.ALL

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "userId")
private User user;

It means that the User has to be deleted after deleting any of Ratings .这意味着在删除任何Ratings后必须删除User

Better to use RatingEntity for the entity and RATINGS for the table name.最好使用RatingEntity作为实体,使用RATINGS作为表名。

General rule一般规则

Never use any cascade with @ManyToOne part of the association!切勿将任何cascade与关联的@ManyToOne部分一起使用!

Also always use fetch = FetchType.LAZY with @ManyToOne .还要始终将fetch = FetchType.LAZY@ManyToOne一起使用。

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "userId")
    private User user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "answer_id")
    private Answer answer;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "question_id")
    private Question question;

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

相关问题 无法删除或更新父行:外键约束失败。 虽然有外键 - Cannot delete or update a parent row: a foreign key constraint fails. Foreign keys exist though 无法删除或更新父行:外键约束失败。 MagicDraw 表格错误 - Cannot delete or update a parent row: a foreign key constraint fails. MagicDraw tables error Rails无法删除或更新父行:外键约束失败 - Rails cannot delete or update a parent row: a foreign key constraint fails #1451-无法删除或更新父行:外键约束失败 - #1451 - Cannot delete or update a parent row: a foreign key constraint fails 无法删除或更新父行:外键约束失败 - MYSQL - Cannot delete or update a parent row: a foreign key constraint fails - MYSQL 无法删除或更新父行:外键约束失败错误 - Cannot delete or update a parent row: a foreign key constraint fails Error 无法删除或更新父行:外键约束失败 - Cannot delete or update a parent row: a foreign key constraint fails Laravel 无法删除或更新父行:外键约束失败 - Laravel Cannot delete or update a parent row: a foreign key constraint fails 错误:无法删除或更新父行:外键约束失败 - Error : Cannot delete or update a parent row: a foreign key constraint fails 无法删除或更新父行:外键约束失败 - Cannot delete or update a parent row: a foreign key constraint fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM