简体   繁体   中英

update or delete on table “users” violates foreign key constraint “fk_owner_id” on table “movie_list”

I have problem deleting a user and cascading deletion to all his movie lists.

class User {
    @OneToMany(mappedBy = "owner", cascade = CascadeType.REMOVE)
    @JsonIgnore
    private List<MovieList> movieLists;
}

class MovieList {
    @ManyToOne
    @JoinColumn(name = "owner_id", foreignKey = @ForeignKey(name = "fk_owner_id"))
    private User owner;
}

This is the method I call when trying to remove a user:

userRepository.delete(user);

When I try to delete a user, I get the following:

update or delete on table "users" violates foreign key constraint "fk_owner_id" on table "movie_list".

How can I solve it?

Thanks

Hello i think you need add attributes in your anotation

Like this:

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)

In other words, it has to pass (CascadeType.ALL, orphanRemoval = true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM