| Project: | Drupal core |
| Version: | 7.x-dev |
| Component: | user system |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed (works as designed) |
Issue Summary
When we have more roles then DRUPAL_AUTHENTICATED, updating user entity in hook_entity_insert cause:
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).
And user is not created.
The reason is in user.module
<?php
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 }
?>in line 595 we execute query without checking if entries exist in db already. Actually adding the same data again changes nothing - we have only primary key in this database.
Originally I've posted this in rules issues que, but now I believe it should be solve in core.
Comments
#1
Simple solution - try catch block around
<?php595 $query->execute();
?>
#2
Further investigation showed that probably it's just rules error.