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

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   gvanto 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 16-August 12

  Posted 16 August 2012 - 05:36 AM

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:

Quote

[03:19:21][root@ps79740:/home/twu5r/public_html/tasker/protected/tests]#phpunit unit/ProjectTest.php
PHPUnit 3.6.12 by Sebastian Bergmann.

Configuration read from /home/twu5r/public_html/tasker/protected/tests/phpunit.xml

Time: 0 seconds, Memory: 11.50Mb

There was 1 error:

1) ProjectTest::testUserAccessBasedOnProjectRole
CDbException: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'member-2' for key 'PRIMARY'

/home/twu5r/public_html/dev3/framework/db/CDbCommand.php:354
/home/twu5r/public_html/dev3/framework/db/CDbCommand.php:1181
/home/twu5r/public_html/dev3/framework/web/auth/CDbAuthManager.php:264
/home/twu5r/public_html/tasker/protected/tests/unit/ProjectTest.php:54

FAILURES!
Tests: 6, Assertions: 9, Errors: 1.



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(
      'projects'=>'Project',
      'users'=>'User',
      'projUsrAssign'=>':tbl_project_user_assignment',
      'projUserRole'=>':tbl_project_user_role',
  );
  
  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'];    
    Yii::app()->user->setId($row1['user_id']);    
    $project = Project::model()->findByPk($row1['project_id']);    
    $this->assertTrue($project->isUserInRole('member'));
  }
  
  public function testUserAccessBasedOnProjectRole() {
    $row1 = $this->projUserRole['row1'];
    Yii::app()->user->setId($row1['user_id']);
    $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:

<?php

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,

gvanto
0

#2 User is offline   gvanto 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 16-August 12

Posted 17 August 2012 - 12:08 AM

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?

Best,
gvanto
0

#3 User is offline   Andro_ 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 17-August 12

Posted 17 August 2012 - 06:39 AM

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

#4 User is offline   gvanto 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 16-August 12

Posted 18 August 2012 - 10:44 PM

Pffft, I was getting ahead of myself! Thanks!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users