Yii 1.1.15 中的外键错误

Foreign Keys error in Yii 1.1.15

我将外键添加到我的应用程序中。我有 commentsgamesfilmsbooks 的 table。当我为游戏创建评论时,我将字段game_id设置为当前游戏的ID,但出现此错误:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`chooseone`.`tbl_comments`, CONSTRAINT `FK_books_comments` FOREIGN KEY (`book_id`) REFERENCES `tbl_books` (`id`) ON DELETE CASCADE ON UPDATE CASCADE). The SQL statement executed was: INSERT INTO `tbl_comments` (`content`, `game_id`, `author_id`, `created`, `updated`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4)

我已经找到相关的帖子,并尝试做一些修改(e.g.change INT(10) in table comments into INT(11) 等),但是错误还在。有人可以帮助我吗?

错误消息包含您解决此问题所需的所有信息:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

以上说明是子行的问题(插入时一般都会这样)

CONSTRAINT `FK_books_comments` FOREIGN KEY (`book_id`) 
REFERENCES `tbl_books` (`id`) ON DELETE CASCADE ON UPDATE CASCADE). 

执行的 SQL 语句是:

INSERT INTO `tbl_comments` (`content`, `game_id`, `author_id`, `created`, `updated`) 
VALUES (:yp0, :yp1, :yp2, :yp3, :yp4)

查看约束。您必须在 tbl_comments table 中的任何行中包含 book_id,并且 book_id 必须与 tbl_books 中记录的 id 相匹配table。但是,您没有在插入语句中包含 book_id

指定 book_id 以解决此问题。