thanks for taking time to look at my thread.
I’m ‘trying to’ create a MySQL table for item categories with a column for the parent category in the table. I linked it back to the table as a fk and intend to insert (0) into the column value for items that have no parents.
Here is the table definition:
-- -----------------------------------------------------
-- Table `mydb`.`itemCategories`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`itemCategories` (
`itecat_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`category` VARCHAR(60) NOT NULL ,
`parentCat` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`itecat_id`) ,
UNIQUE INDEX `uniqueCat` (`category` ASC) ,
INDEX `fk_itemCategories_itemCategories1` (`parentCat` ASC) ,
CONSTRAINT `fk_itemCategories_itemCategories1`
FOREIGN KEY (`parentCat` )
REFERENCES `mydb`.`itemCategories` (`itecat_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
When I try to insert a new row, it gives the following error:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`mydb`.`itemCategories`, CONSTRAINT `fk_itemCategories_itemCategories1` FOREIGN KEY (`parentCat`) REFERENCES `itemCategories` (`itecat_id`) ON DELETE NO ACTION)
Any help on how to fix this, or a better way to do it, would be very much appreciated.
Thanks!
0is a value! You should useNULLas the “empty field”.