【问题标题】:MySQLIntegrityConstraintViolationException: Cannot add or update a child rowMySQLIntegrityConstraintViolationException:无法添加或更新子行
【发布时间】:2014-04-26 19:36:57
【问题描述】:

我的观点 add.scala.html 是这样的

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

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

任何帮助将不胜感激。

【问题讨论】:

    标签: java foreign-keys playframework-2.2


    【解决方案1】:

    你为什么要分配

    shop.products = products;
    

    你不应该这样做

    shop.products.add(products); ?
    

    【讨论】:

      猜你喜欢
      • 2018-03-30
      • 2017-09-27
      • 2016-11-30
      • 1970-01-01
      • 2020-10-19
      • 2014-04-12
      • 1970-01-01
      • 2016-08-18
      • 2017-05-28
      相关资源
      最近更新 更多