SQLSTATE[23000]:完整性约束违反:1452不能添加或更新子行:外键约束失败


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

我在我的web项目中使用cakePHP框架,似乎很多人之前已经问过类似的问题。即使我尝试了,我也找不到答案。

这里是我得到的错误'

数据库错误
Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`fit_or_fat`.`disease_suggestions`, CONSTRAINT `disease_suggestions_ibfk_1` FOREIGN KEY (`disease_id`) REFERENCES `diseases` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
SQL Query: INSERT INTO `fit_or_fat`.`disease_suggestions` (`title`, `suggestion`, `disease_id`, `modified`, `created`) VALUES ('cholestorol', 'dddddddddddddddd', 'dd', '2014-08-24 00:04:32', '2014-08-24 00:04:32') 

这是我的模型

'disease_id' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'disease_id must not be empty',
            ),
            'custom' => array(
                'rule' => '/['w's'd., '-_]+/',
                'message' => 'user_id can only contain simple and capital letters, 0-9 numbers, . , - _ space and tabs only.',
            ),
        ),

您似乎正在将dd添加到疾病id列中。这是有限制的。大多数情况下,ID列都是数字。所以这个警告是告诉你查询是错误的,或者特别地试图插入不允许的值。

INSERT INTO `fit_or_fat`.`disease_suggestions` (`title`, `suggestion`, `disease_id`, `modified`, `created`) VALUES ('cholestorol', 'dddddddddddddddd', 1, '2014-08-24 00:04:32', '2014-08-24 00:04:32') 

很可能会被允许(我不知道ID 1上有什么疾病,但这是另一个问题)