DaviesTobi alex
DaviesTobi alex

Reputation: 670

Cannot delete or update a parent row a foreign key constraint fails

Cannot delete or update a parent row: a foreign key constraint fails (gopick.orders_paid_items, CONSTRAINT FKeeefhbl6j5xhs7nnt5mn530f8 FOREIGN KEY (paid_items_product_id, paid_items_user_id) REFERENCES cart (product_id, user_id))

Cart Entity Below

    @Entity
    @ToString
    @EqualsAndHashCode
    @IdClass(CartIdPk.class)
    public class Cart implements Serializable {

    @Column(unique = true)
    private Long id = Long.parseLong(String.format("%06d", new Random().nextInt(999999)));

    @JsonIgnore
    @Id
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    private CartStatus cartStatus = CartStatus.IN_CART;

    @Id
    private int productId;

    private int quantity = 0;

    @Column(length = 10, nullable = true)
    private String discount;

    @Column(length = 30, nullable = true)
    private String paymentRef;

    @JsonIgnore
    @Column(insertable = false, updatable = true)
    @UpdateTimestamp
    private Timestamp lastModified;

    @CreationTimestamp
    private Timestamp dateCreated;

}

Id class Object CartIdPk

@NoArgsConstructor
@AllArgsConstructor
@lombok.Data
public class CartIdPk implements Serializable {

    private Long user;

    private int productId;
}

Orders Entity Below

    @lombok.Data
    @Entity
    public class Orders implements Serializable {

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

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    @OneToMany(fetch = FetchType.LAZY)
    private Collection<Cart> paidItems;

    @JsonIgnore
    @Column(insertable = false, updatable = true)
    @UpdateTimestamp
    private Timestamp lastModified;

    @CreationTimestamp
    private Timestamp dateCreated;

}

When I try to delete from the Cart Entity Table it works perfectly if the productId is not present in the generated orders_paid_items but if it is, it throws an error as stated above.

Deleting an item from the cart table using the default CrudRepository delete method as shown below

 cartDataService.remove(cartDataService.find(cartId));

Upvotes: 0

Views: 2083

Answers (1)

Michael Wiles
Michael Wiles

Reputation: 21194

The rule of thumb when using JPA and ORMs in java is to remember that you have both an object model and a relational database model and you have to make the changes in both.

So you have a 1 to many between Orders and Cart.

So for argument sake, you try to delete a Cart that is currently in an order. When the save happens you're going to get an integrity violation as that Cart you are trying to delete is still referenced by an order.

What you have to do is to remove the orders from the card before you delete because you're referencing the Cart in the orders.

Always try to do the change at the "object" level and at the database level.

Upvotes: 1

Related Questions