Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"

RegistrationId class is a composite key used in Registration class. I'm using Spring data jpa and 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.

@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;
like image 791
Merv Avatar asked Jun 15 '17 20:06

Merv


3 Answers

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.

like image 195
Moshe Arad Avatar answered Sep 25 '22 13:09

Moshe Arad


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.

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; 
like image 38
Merv Avatar answered Sep 23 '22 13:09

Merv


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.

like image 36
JoshKopen Avatar answered Sep 25 '22 13:09

JoshKopen