I don't want to have to write a php
recursive function to handle it.
I have a lookup table which generates a list of articles and categories, each has it's own unique id (nav_id
) and if it's a child it has a parent_id
(articles are never used as a parent however).
So my thoughts were CASCADE DELETE, which keeps running into violation parent_id cannot be null. However this is how I identify root.
SQL
CREATE TABLE IF NOT EXISTS `navigation` (
`nav_id` INT NOT NULL AUTO_INCREMENT COMMENT '',
`parent_id` INT NULL COMMENT '',
`article_id` INT NULL DEFAULT 0 COMMENT '',
`category_id` INT NULL DEFAULT 0 COMMENT '',
`position` TINYINT NULL COMMENT '',
PRIMARY KEY (`nav_id`, `parent_id`) COMMENT '',
UNIQUE INDEX `nav_id_UNIQUE` (`nav_id` ASC) COMMENT '',
INDEX `rec1_idx` (`parent_id` ASC) COMMENT '',
CONSTRAINT `rec1`
FOREIGN KEY (`parent_id`)
REFERENCES `navigation` (`nav_id`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB
However INSERT result for:
':parent_id' => null
':article_id' => null
':category_id' => 19
':position' => 1
INSERT INTO navigation (
parent_id,article_id,category_id,position
) VALUES (
:parent_id,:article_id,:category_id,:position
)
is
Integrity constraint violation: 1048 Column 'parent_id' cannot be null
Any and all ideas are welcome.
Table structural change? Foreign key change?
I also tried a trigger on after and before delete however you can't delete from the same table the trigger is executed on.