postgresql 错误:更新或删除表“tablename”违反外键约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44576191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:26:41  来源:igfitidea点击:

ERROR: update or delete on table "tablename" violates foreign key constraint

postgresqlspring-bootspring-data-jpa

提问by Merv

I'm trying to delete the parent student or parent course and I get this error:

我正在尝试删除家长学生或家长课程,但出现此错误:

Caused by: org.postgresql.util.PSQLException: ERROR: update or delete on table "student" violates foreign key constraint "fkeyvuofq5vwdylcf78jar3mxol" on table "registration"

引起:org.postgresql.util.PSQLException:错误:更新或删除表“student”违反表“注册”上的外键约束“fkeyvuofq5vwdylcf78jar3mxol”

RegistrationId class is a composite key used in Registration class. I'm using Spring data jpa and spring boot.

RegistrationId 类是 Registration 类中使用的复合键。我正在使用 Spring data jpa 和 spring boot。

What am I doing wrong? I know that putting cascadetype.all should also remove the children when the parent is deleted but it is giving me an error instead.

我究竟做错了什么?我知道在删除父级时放置级联类型.all 也应该删除子级,但它给了我一个错误。

@Embeddable
public class RegistrationId implements Serializable {

  @JsonIgnoreProperties("notifications")
  @OneToOne(cascade=CascadeType.ALL)
  @JoinColumn(name = "student_pcn", referencedColumnName="pcn")
  private Student student;

  @JsonIgnoreProperties({"teachers", "states", "reviews"})
  @OneToOne(cascade=CascadeType.ALL)
  @JoinColumn(name = "course_code", referencedColumnName="code")
  private Course course;


Registration class


注册类

@Entity(name = "Registration")
@Table(name = "registration")
public class Registration {

@EmbeddedId
private RegistrationId id;

采纳答案by Merv

I made it work by using hibernate @OnDelete annotation. Some how the JPA.persistence CascadeTypes were not working. They had no effect for whichever I chose.

我通过使用 hibernate @OnDelete 注释使它工作。JPA.persistence CascadeTypes 不工作的一些原因。无论我选择哪个,它们都没有影响。

Just like below. Now I can remove the parent Student or the parent Course and all children(Registrations) are deleted with them.

就像下面一样。现在我可以删除父学生或父课程,并且所有孩子(注册)都被删除。

@Embeddable
public class RegistrationId implements Serializable {

    @JsonIgnoreProperties("notifications")
    @OnDelete(action = OnDeleteAction.CASCADE)
    @OneToOne
    @JoinColumn(name = "student_pcn", referencedColumnName="pcn")
    private Student student;

    @JsonIgnoreProperties({"teachers", "states", "reviews"})
    @OnDelete(action = OnDeleteAction.CASCADE)
    @OneToOne
    @JoinColumn(name = "course_code", referencedColumnName="code")
    private Course course;

回答by Moshe Arad

When you're using a relational DB, you are setting entities with relationships between these entities.

当您使用关系数据库时,您正在设置具有这些实体之间关系的实体。

The error that you're getting means that:

你得到的错误意味着:

You're trying to delete a record that its primary key is functioning as a foreign key in another table, thus you can't delete it.

您试图删除一条主键在另一个表中用作外键的记录,因此您无法删除它。

In order to delete that record, first, delete the record with the foreign key, and then delete the original that you wanted to delete.

为了删除该记录,首先删除带有外键的记录,然后删除您要删除的原始记录。

回答by JoshKopen

Foreign keys guarantee that an entry will exist in another table. This is a way of ensuring data integrity. SQL will never allow you to delete this entry while it still deletes in the other table. Either (1) this is letting you know you would have made a grave mistake by deleting this thing which is required or (2) you would like to put in a cascading delete so that not only is this entry deleted but so is what is supposed to be referencing it in the other table. Information on cascading deletes can be found here and written fairly easily (https://www.techonthenet.com/sql_server/foreign_keys/foreign_delete.php). If neither of these two descriptions fits you, evaluate why your foreign key relationship exists in the first place because it probably should not.

外键保证一个条目将存在于另一个表中。这是确保数据完整性的一种方式。SQL 永远不会允许你删除这个条目,而它仍然在另一个表中删除。要么 (1) 这让你知道你会因为删除这个必需的东西而犯了一个严重的错误,或者 (2) 你想进行级联删除,这样不仅这个条目被删除,而且应该被删除在另一个表中引用它。可以在此处找到有关级联删除的信息,并且很容易编写 ( https://www.techonthenet.com/sql_server/foreign_keys/foreign_delete.php)。如果这两种描述都不适合您,请首先评估为什么您的外键关系存在,因为它可能不应该存在。