Integrity constraint violation: 1062 Duplicate entry 'member-2' for key 'PRIMARY'

I’m on page 199 of the Agile Web Dev book (which is great, must say) but now I’ve gotten stuck when I run my test i get this error:

I (think) have followed everything exactly as per the instructions. (all unit tests previously have passed).

First part of ProjectTest.php:

class ProjectTest extends CDbTestCase {


  public $fixtures = array(







  public function testUserRoleAssignment() {

    $project = $this->projects('project1');

    $user = $this->users('user1');

    $this->assertEquals(1, $project->associateUserToRole('owner', $user->id));

    $this->assertEquals(1, $project->removeUserFromRole('owner', $user->id));



  public function testIsInRole() {

    $row1 = $this->projUserRole['row1'];    


    $project = Project::model()->findByPk($row1['project_id']);    




  public function testUserAccessBasedOnProjectRole() {

    $row1 = $this->projUserRole['row1'];


    $project = Project::model()->findByPk($row1['project_id']);


    //Assign php snippet bizRule

    $auth = Yii::app()->authManager;

    $bizRule = 'return isset($params["project"]) && $params["project"]->isUserInRole("member");';

    $auth->assign('member', $row1['user_id'], $bizRule); //line 44

    $params = array('project'=>$project);


    $this->assertTrue(Yii::app()->user->checkAccess('updateIssue', $params));

    $this->assertTrue(Yii::app()->user->checkAccess('readIssue', $params));

    $this->assertFalse(Yii::app()->user->checkAccess('updateProject', $params));


My tbl_project_user_role.php file:


return array(

    'row1' => array(

        'project_id' => 2,

        'user_id' => 2,

        'role' => 'member',




Looking at the error, it seems that it’s trying to insert a record but that primary key is already been duplicated.

I’ve tried a couple of things:

  • Have the fixtures file return an empty array:

This STILL causes same issue

  • Deleted the (only) record from the tbl_project_user_role table

Still get same issue

I am not quite sure what I am missing here?

Any help much appreciated,


OK I found the issue, the AuthAssignment table needs to be cleared of all records for the test to pass.

If a SQL query to do this can be run at the start of running the unit test that would be awesome.

Anyone know how to configure this ‘pre-test’ sequence?



On page 201 there is a solution to your problem. Basically you have to to the same thing as with the tbl_project_user_role. Ad a fixture that returns an empty array and then add that fixture to the test.

Pffft, I was getting ahead of myself! Thanks!

I am new to Yii I am in chapter 7 of the book(web application development with yii and php) and I managed to solve the problem of duplication an authassinment able.I hope it is a good solution for this error:

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate … by using isAssigned methos of authManager in assign method in ProjectUserForm.php model like this:

public function assign()


if($this->_user instanceof User)


//assign the user, in the specified role, to the project

$this->project->assignUser($this->_user->id, $this->role);

//add the association, along with the RBAC biz rule, to our RBAC hierarchy

$auth = Yii::app()->authManager;

$bizRule=‘return isset($params[“project”]) && $params[“project”]->allowCurrentUser("’.$this->role.’");’;


$auth->assign($this->role,$this->_user->id, $bizRule);

return true;




$this->addError(‘username’,‘Error when attempting to assign this user to the project.’);

return false;



so in the if statement {if(!$auth->isAssigned($this->role,$this->_user->id))} , if the user has owner role assigned t them prior this,it won’t be duplicated and project will be assigned to user as owner and they are also available to add different users by different roles to that specific project.