"CDbCommand failed to execute the SQL statement: SQLSTATE[23000]:" Example from book

StackOverflow https://stackoverflow.com/questions/18273993

  •  24-06-2022
  •  | 
  •  

Вопрос

Good day! Faced with such a problem, when creating Issue (http://test.com/book/ima/index.php?ssue/create&pid=1) get this error:

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         {
...

I tried to do as here: 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'),
        .....
        ........
    );
}

It did not work. Help solve the problem, I want to study further, but because of it I can not.

Это было полезно?

Решение

From the link and error you have given, there was constraint violation could be due to:

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

The error message was so obviously. You tried to insert into tbl_issue one record which contains the foreign key user_id does not exist on tbl_user.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top