我有一个客户和产品的映射表。以下是步骤
create table `customer_products` (
`customer_id` bigint not null,
`product_id` bigint not null,
primary key (`customer_id`, `product_id`)
);
alter table `customer_products`
add constraint `FK7urin54lem7yxy6umxf899t16`
foreign key (`customer_id`)
references `customer` (`customer_id`);
alter table `customer_products`
add constraint `FKtfgjfwfykaef4wjk00ofyqq8y`
foreign key (`product_id`)
references `product` (`product_id`);
insert into customer_products values(7,5); //should get a contraint error
当我插入到这个映射表中时,尽管父表中没有相应的条目,但我在上面的语句中没有insert
错误。我是否需要一些额外的选项来放置此约束?
这个方法不好。 但是有一个解决方案。
You can write a trigger on customer_products and set 'before insert'
您需要编写更多智能查询以插入到不存在的父表中。
我明白了。JPA使用MyISAM引擎来创建表,它不支持外键约束。因此,一旦我将引擎修改为InnoDB,问题就解决了。