2014-04-26 55 views
2

我的看法是add.scala.html這 enter image description hereMySQLIntegrityConstraintViolationException:不能添加或更新子行

所以,如果我的產品添加它顯示了相同的屏幕上,我有一個按鈕來刪除product.My問題是,當我添加新的產品,它工作正常,但是當我刪除任何一個再經過添加新的產品它給我的錯誤

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

任何幫助將非常感激。

回答

0

你爲什麼分配

shop.products = products; 

應該不是你做

shop.products.add(products); ? 
相關問題