Question

My view add.scala.html is this enter image description here

So if i add a product it shows on same screen and i have a delete button to delete product.My problem is that when i add new product it works fine but when i add a new product after deleting any one then it gives me the error

[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`))]

on my 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();
    }

my database

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;

Any help would be really appreciated.

Was it helpful?

Solution

Why are you assigning

shop.products = products;

shouldn't you do

shop.products.add(products); ?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top