[SOLVED]CH8 - RBAC test fails on page 199

1) ProjectTest::testDelete

CDbException: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`trackstar_test`.`tbl_project_user_role`, CONSTRAINT `tbl_project_user_role_ibfk_1` FOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`))

C:\inetpub\wwwroot\Yii 1.1.5\framework\db\CDbCommand.php:256

C:\inetpub\wwwroot\Yii 1.1.5\framework\db\ar\CActiveRecord.php:1647

C:\inetpub\wwwroot\Yii 1.1.5\framework\db\ar\CActiveRecord.php:1113

C:\inetpub\wwwroot\Yii 1.1.5\trackstar\protected\tests\unit\ProjectTest.php:75


Tests: 7, Assertions: 14, Errors: 1.

On page 199, at the top of the page when I modify the testIsInRole() function I am getting the above error when I run the ProjectTest.php.

ProjectTest.php line 75 refers to this code:


All my tests ran flawlessly up until this point, can someone give me a pointer as to what I might be doing wrong here?

The foreign keys defined on the db on which these were tested have the following cascading definitions:


Without this configured in your schema, then the delete would need to be altered to explicitly delete related content before deleting the primary row to which the other data is related.

The book is making an attempt to be database agnostic, and glosses over some of the schema specific details. The download-able code has the full MySQL schema with these definitions in-place.

I cannot believe I overlooked the fact that FK constraints were not in my DDL file. What’s more embarrassing is that the plain English mySQL error in my face!!!

Thank you very much for this great resource Jeff. This is my first attempt to learn a framework and I’m really enjoying this experience though I might have to go through one more time. And the lack of sleep and caffeine is definitely not helping that’s for sure…


Right DDL:

create table tbl_project_user_role


	project_id INTEGER NOT NULL,



	primary key (project_id, user_id,role),

	foreign key (project_id) references tbl_project (id) ON DELETE CASCADE ON UPDATE CASCADE,

	foreign key (user_id) references tbl_user (id) ON DELETE CASCADE ON UPDATE CASCADE,

	foreign key (role) references AuthItem (name) ON DELETE CASCADE ON UPDATE CASCADE


How Do configure this please help?