插入或更新表“products”违反外键(Postgresql spring boot)错误

问题描述 投票:0回答:2

我在 spring boot 中有一个 api,我决定创建一个模型订单,但我发现了一些数据库错误,我决定进入我的 pg admin 并删除所有表并重新开始,但是现在当我请求我的模型产品时,我发现错误:

2023-03-27T15:00:48.189-03:00  WARN 31677 --- [nio-8011-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 23503
2023-03-27T15:00:48.190-03:00 ERROR 31677 --- [nio-8011-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: insert or update on table "products" violates foreign key constraint "fkselc31gul05wygg2llkv0v3yb"
  Detail: Key (product_id)=(9c2655d0-c5a4-45f1-ba97-8b5fe1a385d8) is not present in table "orders".
2023-03-27T15:00:48.231-03:00  INFO 31677 --- [nio-8011-exec-6] o.h.e.j.b.internal.AbstractBatchImpl     : HHH000010: On release of batch it still contained JDBC statements

我已经尝试删除所有表,我也尝试创建另一个数据库并删除旧的,但我仍然得到同样的错误

订单型号

package com.api.business_manager_api.Models;

import jakarta.persistence.*;

import java.util.List;
import java.util.UUID;

@Entity
@Table(name = "ORDERS")
public class OrderModel {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private UUID order_id;
    @ManyToOne
    @JoinColumn(name = "customer_id")
    private CustomerModel customer;
    @OneToMany
    @JoinColumn(name = "product_id")
    private List<ProductModel> products;
    @Column(nullable = false, length = 80)
    private Float totalAmount;

    public UUID getOrder_id() {
        return order_id;
    }

    public void setOrder_id(UUID order_id) {
        this.order_id = order_id;
    }

    public CustomerModel getCustomer() {
        return customer;
    }

    public void setCustomer(CustomerModel customer) {
        this.customer = customer;
    }

    public List<ProductModel> getProducts() {
        return products;
    }

    public void setProducts(List<ProductModel> products) {
        this.products = products;
    }

    public Float getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(Float totalAmount) {
        this.totalAmount = totalAmount;
    }
}

产品型号

package com.api.business_manager_api.Models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;

import java.util.UUID;

@Entity
@Table(name = "PRODUCTS")
public class ProductModel {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private UUID product_id;

    @Column(nullable = false, length = 80)
    private String product;
    @Column(nullable = true, length = 200)
    private String image_url;
    @Column(nullable = true, length = 80)
    private String brand;
    @Column(nullable = true, length = 80)
    private String description;
    @Column(nullable = true, length = 80)
    private Float price;

    @Column(nullable = true, length = 80)
    private Float extraPrice;

    @Column(nullable = false, length = 80)
    private Integer stock;
    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "category_id")
    private CategoryModel productCategory;

    public UUID getProduct_id() {
        return product_id;
    }

    public void setProduct_id(UUID product_id) {
        this.product_id = product_id;
    }

    public String getProduct() {
        return product;
    }

    public void setProduct(String product) {
        this.product = product;
    }

    public String getImage_url() {
        return image_url;
    }

    public void setImage_url(String image_url) {
        this.image_url = image_url;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public Integer getStock() {
        return stock;
    }

    public void setStock(Integer stock) {
        this.stock = stock;
    }

    public CategoryModel getProductCategory() {
        return productCategory;
    }

    public void setProductCategory(CategoryModel productCategory) {
        this.productCategory = productCategory;
    }

    public Float getExtraPrice() {
        return extraPrice;
    }

    public void setExtraPrice(Float extraPrice) {
        this.extraPrice = extraPrice;
    }
}

我不知道会有什么反应,因为正如你们所看到的,我的产品不依赖于任何要创建的订单

java postgresql spring-boot api pgadmin
2个回答
0
投票

我认为您正在尝试修改产品 ID,并且您使用的数据库中的错误具有误导性:

ERROR: insert or update on table "products" violates foreign key constraint "fkselc31gul05wygg2llkv0v3yb"
Detail: Key (product_id)=(9c2655d0-c5a4-45f1-ba97-8b5fe1a385d8) is not present in table "orders".

我认为因为

orders
List<ProductModel> products
中引用产品,它告诉你
in table "orders"
foreign key constraint
被试图改变
(product_id)=(9c2655d0-c5a4-45f1-ba97-8b5fe1a385d8)
的行为所侵犯。

如果你想更改产品 ID,那么你还必须使这些更改级联。


0
投票

我相信你在这里试图做的是在订单和产品表之间建立单向的一对多关系,即一个订单可能有多个产品。 因此,理想情况下,您的连接列应该类似于 order_id 而不是 product_id,因为这将是 PRODUCTS 表中引用 ORDERS 表的 order_id 条目的 FK。更改 Join 列后,您还可以添加 cascading,以便根据您的用例将 ORDERS 表中的更改也级联到 PRODUCTS 表中。

这里有一些很好的参考资料,您可以在使用 Hibernate 和 Springboot 创建单向一对多关系时参考。

https://www.bezkoder.com/jpa-one-to-many-unidirectional/

此外,如果您想要 ProductModel 对象中的订单信息,我建议您使用 ProductModel 类中的 OrderModel 成员创建一个 OneToMany 双向关系

© www.soinside.com 2019 - 2024. All rights reserved.