Reproduced on clean installation.

It works when I just have "After saving a new user account" and either "User has role(s)" or "Set a data value". All tree together breaks user creation with error:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '24-4' for key 'PRIMARY': INSERT INTO {users_roles} (uid, rid) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1); Array ( [:db_insert_placeholder_0] => 24 [:db_insert_placeholder_1] => 4 ) in user_save() (line 595 of /modules/user/user.module).

While testing I was always creating user from admin/people/create.

I figured out that actually roles that we are checking matters:

  • creating 'authenticated'+'admin'; checking if has 'admin' - got error
  • creating 'authenticated'; checking if has 'authenticated' - works ok
  • creating 'authenticated'; checking if has any of 'authenticated', 'admin' - works ok
  • creating 'authenticated'+'admin'; checking if has any of 'authenticated', 'admin' - got error

On developement site I've got the same error while checking for other roles

Comments

marcin.wosinek’s picture

Component: Rules Core » Rules Engine

In user.module we have

 582       module_invoke_all('entity_insert', $account, 'user');
 583
 584       // Save user roles.
 585       if (count($account->roles) > 1) {
 586         $query = db_insert('users_roles')->fields(array('uid', 'rid'));
 587         foreach (array_keys($account->roles) as $rid) {
 588           if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
 589             $query->values(array(
 590               'uid' => $account->uid,
 591               'rid' => $rid,
 592             ));
 593           }
 594         }
 595         $query->execute();
 596       }

So it seems that changing data in user entity activate the same function twice; and as we don't check if a role has been already added we get this error

marcin.wosinek’s picture

User creation fail when created user has any additional role, and you change it's data. This is enough to run roles updating code twice.

Any way, as 'users_roles' table has only two columns, and both are primary key; there is absolutely no difference how many times we add the same stuff there.

marcin.wosinek’s picture

Status: Active » Closed (works as designed)

After investigating more this error, I see that rules implementation do it properly. The error is always when we are saving user with more then one role within entity_save implementation. So it should be fixed in user.module:
http://drupal.org/node/1433288