gpt4 book ai didi

java - MySQLIntegrityConstraintViolationException : Cannot add or update a child row

转载 作者:行者123 更新时间:2023-11-29 08:49:52 26 4
gpt4 key购买 nike

我的观点add.scala.html是这样的 enter image description here

因此,如果我添加一个产品,它会显示在同一屏幕上,并且我有一个删除按钮来删除产品。我的问题是,当我添加新产品时,它工作正常,但是当我在删除任何产品后添加一个新产品时,它会给出我的错误

[PersistenceException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`shopdb`.`product_shop`, CONSTRAINT `fk_product_shop_product_01` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`))]

在我的 models.Product.java 上

public static Product create(Product product,Shop shop) {
product.save();

static List<Product> products = new ArrayList<>();
products.add(product);

shop.products = products;
shop.save();//getting error on this line




return product;
}
public static void delete(Long id) {
find.ref(id).delete();
}

我的数据库

create table product (
id bigint auto_increment not null,
name varchar(255),
price float,
category varchar(255),
constraint pk_product primary key (id))
;

create table shop (
id bigint auto_increment not null,
name varchar(255),
address_line1 varchar(255),
address_line2 varchar(255),
address_line3 varchar(255),
city varchar(255),
town varchar(255),
phone_number varchar(255),
category varchar(255),
shop_pic longblob,
owner_id bigint,
constraint pk_shop primary key (id))
;

create table product_shop (
product_id bigint not null,
shop_id bigint not null,
constraint pk_product_shop primary key (product_id, shop_id))
;

alter table product_shop add constraint fk_product_shop_product_01 foreign key (product_id) references product (id) on delete restrict on update restrict;

alter table product_shop add constraint fk_product_shop_shop_02 foreign key (shop_id) references shop (id) on delete restrict on update restrict;

非常感谢任何帮助。

最佳答案

为什么要分配

shop.products = products;

你不应该这样做吗

shop.products.add(products); ?

关于java - MySQLIntegrityConstraintViolationException : Cannot add or update a child row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23315617/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com