drkj41932 2014-08-23 10:23
浏览 359
已采纳

SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键约束失败

I am using cakePHP framework in my web project,It seems many people have already ask similar question before.Even I try for those I couldn't find the answer.

Here is the error I got'

Database Error

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') 

This is my model

'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.',
            ),
        ),
  • 写回答

1条回答 默认 最新

  • doujiang5211 2014-08-23 10:32
    关注

    You appear to be adding dd to the disease_id column. Which has a constraint. Most often ID columns are numerical. So this warning is telling you the the query is wrong, or specifically trying to insert values not allowed.

    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') 
    

    Will most likely be allowed (I have no idea what disease is on ID 1 but that's another issue all together).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100