我正在使用 Codeigniter 并想在帖子和评论表之间添加关系。
帖子:
CREATE TABLE IF NOT EXISTS `posts` (
`post_id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`slug` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`content` text COLLATE utf8_persian_ci NOT NULL,
`time` varchar(10) COLLATE utf8_persian_ci NOT NULL,
`num_of_comments` int(3) NOT NULL,
`cat` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`tags` varchar(100) COLLATE utf8_persian_ci NOT NULL,
PRIMARY KEY (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=15 ;
注释:
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`content` text COLLATE utf8_persian_ci NOT NULL,
`date` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`time` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`name` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`email` varchar(30) COLLATE utf8_persian_ci NOT NULL,
`image` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`ip` varchar(16) COLLATE utf8_persian_ci NOT NULL,
`submit` int(1) NOT NULL,
`reply` text COLLATE utf8_persian_ci NOT NULL,
`reply_date` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`reply_time` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`post_id` int(5) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `post_id` (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=26 ;
但它会打印此错误:
SQL query:
ALTER TABLE `comments` ADD FOREIGN KEY ( `post_id` ) REFERENCES `codeig`.`posts` (
`post_id`
) ON DELETE CASCADE ON UPDATE CASCADE ;
MySQL said:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`codeig`. <result 2 when explaining filename '#sql-fc8_dd'>, CONSTRAINT `#sql-fc8_dd_ibfk_1` FOREIGN KEY (`post_id`) REFERENCES `posts` (`post_id`) ON DELETE CASCADE ON UPDATE CASCADE)
我根本不知道在说什么。
请帮忙...