Working with foreign keys - cannot insert

Posted by Industrial on Stack Overflow See other posts from Stack Overflow or by Industrial
Published on 2010-03-28T11:56:05Z Indexed on 2010/03/28 12:23 UTC
Read the original article Hit count: 234

Hi everyone!

Doing my first tryouts with foreign keys in a mySQL database and are trying to do a insert, that fails for this reason: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

Does this mean that foreign keys restrict INSERTS as well as DELETES and/or UPDATES on each table that is enforced with foreign keys relations?

Thanks!

Updated description:

Products
----------------------------
id | type
----------------------------
0     | 0
1     | 3

ProductsToCategories
----------------------------
productid | categoryid
----------------------------
0        | 0
1        | 1

Product table has following structure

CREATE  TABLE IF NOT EXISTS `alpha`.`products` (
  `id` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `type` TINYINT(2) UNSIGNED NOT NULL DEFAULT 0 ,    
  PRIMARY KEY (`id`) ,  
  CONSTRAINT `prodsku`  
    FOREIGN KEY (`id` )
    REFERENCES `alpha`.`productsToSku` (`product` )    
    ON DELETE CASCADE,    
    ON UPDATE CASCADE)  

ENGINE = InnoDB;

© Stack Overflow or respective owner

Related posts about mysql

Related posts about foreign-keys