【问题标题】:"CDbCommand failed to execute the SQL statement: SQLSTATE[23000]:" Example from book“CDbCommand 未能执行 SQL 语句:SQLSTATE[23000]:”书中的示例
【发布时间】:2013-08-18 21:36:12
【问题描述】:

美好的一天! 遇到这样的问题,在创建Issue(http://test.com/book/ima/index.php?ssue/create&pid=1)的时候得到这个错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`trackstar`.`tbl_issue`, CONSTRAINT `fk_issue_owner` FOREIGN KEY (`owner_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO `tbl_issue` (`project_id`, `name`, `description`, `type_id`, `status_id`, `owner_id`, `requester_id`, `create_time`, `create_user_id`, `update_time`, `update_user_id`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6, :yp7, :yp8, :yp9, :yp10)

/var/www/yii/framework/db/CDbCommand.php(357)

345         {
...

我尝试这样做: http://www.yiiframework.com/forum/index.php/topic/11953-cdbcommand-failed-to-execute-the-sql-statement-sqlstate23000-integrity-constraint-violation/

 public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('name, project_id', 'required'),
        .....
        ........
    );
}

它没有工作。 帮助解决问题,我想进一步研究,但因为它我不能。

【问题讨论】:

    标签: php yii mysql-error-1452


    【解决方案1】:

    link 和您给出的错误来看,存在约束违规可能是由于:

    The user_id attempting to be inserted is not associated with a valid tbl_user.id value in the tbl_user table.
    

    错误信息是如此明显。您试图在tbl_issue 中插入一条包含外键user_id 的记录在tbl_user 上不存在。

    【讨论】:

    • 谢谢。很简单!
    • 如果我的上述答案解决了您的问题,请将其标记为已接受。很高兴为您提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    相关资源
    最近更新 更多