Witam
Podczas próby zapisu do bazy danych, otrzymuje następujący błąd:
Cannot add or update a child row: a foreign key constraint fails (
flycms.
menuitems, CONSTRAINT
menuitems_ibfk_1 FOREIGN KEY (
menugroup_id) REFERENCES
menugroups (
id) ON DELETE CASCADE) [ INSERT INTO
menuitems (
name,
title,
page_id,
created,
link) VALUES ('ewqewqeewew', '', '31', 1274733606, '') ]
Struktura tabeli menuitems:
CREATE TABLE IF NOT EXISTS `menuitems` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`menugroup_id` int(11) NOT NULL,
`parent_id` int(11) DEFAULT NULL,
`page_id` int(11) DEFAULT NULL,
`type` tinyint(4) NOT NULL,
`name` varchar(100) NOT NULL,
`link` varchar(100) NOT NULL,
`title` varchar(100) NOT NULL,
`ord` tinyint(4) NOT NULL,
`lft` int(11) NOT NULL,
`rgt` int(11) NOT NULL,
`scope` int(11) NOT NULL DEFAULT '0',
`lvl` int(11) NOT NULL DEFAULT '0',
`created` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `group_id` (`menugroup_id`),
KEY `page_id` (`page_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
ALTER TABLE `menuitems`
ADD CONSTRAINT `menuitems_ibfk_2` FOREIGN KEY (`page_id`) REFERENCES `pages` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `menuitems_ibfk_1` FOREIGN KEY (`menugroup_id`) REFERENCES `menugroups` (`id`) ON DELETE CASCADE;
</mysql>
I pages:
```mysql
CREATE TABLE IF NOT EXISTS `pages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`link` varchar(100) NOT NULL,
`keywords` tinytext,
`description` tinytext,
`theme_id` int(11) unsigned DEFAULT NULL,
`is_main` tinyint(4) NOT NULL DEFAULT '0',
`header_on` tinyint(4) DEFAULT NULL,
`footer_on` int(11) DEFAULT NULL,
`sidebar_on` int(11) DEFAULT NULL,
`created` int(11) NOT NULL,
`last_modified` int(11) DEFAULT NULL,
`author` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `title` (`title`,`link`),
UNIQUE KEY `link` (`link`),
KEY `template_id` (`theme_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=32 ;
W tabeli menuitems, domyślnie page_id mam ustawiony na null, ponieważ nie zawsze dany odnośnik może być związany z jakąś stroną w systemie.
Wyjaśni mi ktoś genezę tego błędu?