philb
November 27, 2011, 2:58pm
1
Hello Guys
I’m trying to set up a database with two tables and use foreign keys.
I’ve set the DB to InnoDB and have got some success but I try to insert anything into ‘product’ table I get an error message
#1452 - Cannot add or update a child row: a foreign key constraint fails (philb_f0ldable/product, CONSTRAINT product_ibfk_1 FOREIGN KEY (productid) REFERENCES merchants (productid) ON DELETE CASCADE ON UPDATE CASCADE)
My set up is
Table 1
product
productid
productname
productdescription
Table 2
merchants
merchantid
productid foreign key to product.productid
productlink
Can anyone help me with this?
The error message says that the constraint is from the product table to the merchants table, the opposite of what you states in your table description. Can you show the create table statements that you used?
philb
November 27, 2011, 4:37pm
3
Hi swampBoogie
So i need the constraint from the merchant table to the product table, but I’ve set it up wrong in phpmyadmin, figures.
I created the table using phpmyadmin, entered the details manually.
philb
November 27, 2011, 4:53pm
4
I think I may be getting it fixed. I’ve used
ALTER TABLE merchants
ADD CONSTRAINT FK_merchants
FOREIGN KEY (productid) REFERENCES product(productid)
ON UPDATE CASCADE
ON DELETE CASCADE
And it’s letting me insert into the products table