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

I am trying to delete a parent student or parent course and I am getting this error:

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

The RegistrationId class is a composite key used in the registration class. I am using Spring data jpa and Spring boot.

What am I doing wrong? I know that when setting cascadetype.all, it should also remove children when parent is removed, but instead it gives me an error.

@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;

      

+3


source to share


3 answers


I did this using the hibernate @OnDelete annotation. Some of them didn't work with JPA.persistence CascadeTypes methods. They did not affect what I chose.

As below. Now I can delete the parent Student or the parent course and all the children (registrations) will be deleted along 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;

      

+3


source


When you use a relational database, you set up objects with relationships between those objects.

The error you are getting means that:



You are trying to delete a record where its primary key acts as a foreign key in another table , so you cannot delete it.

To delete this record , first delete the record using the foreign key and then delete the original that you want to delete.

+2


source


Foreign keys guarantee that a record will exist in another table. This is a way to ensure data integrity. SQL will never let you delete this record while it is still being deleted in another table. Either (1) it makes you understand that you would be making a serious mistake by removing that thing that is required, or (2) you would like to add a cascading delete so that not only this entry is deleted, but what is supposed to be referenced him in another table. Information on cascading deletes can be found here and written quite easily ( https://www.techonthenet.com/sql_server/foreign_keys/foreign_delete.php ). If neither of these two descriptions works for you, evaluate the reason your foreign key relationship exists in the first place, because it probably shouldn't.

0


source







All Articles