If you perform more than one action on a new user, you get a
"PDOException: SQLSTATE[23000]: Integrity constraint violation ... " in user_save() (... /modules/user/user.module)
Try it yourself:
Event - After saving a new user account
Action - Add user role
Action - Unblock a use (should be user?)
The above will cause an error, while having just one action won't.
See #1169800: [Integrity constraint violation: 1062 Duplicate entry] On user registration for more people having this problem.
For what it's worth, this workaround prevents the crash:
Go to admin/config/workflow/rules and look for any rule that does something to a user.
If any of those tasks do more than one edit to a user, try to create actions in that rule which:
- Perform first user action
- Add a variable (convert the user to a UID (integer) variable)
- Add a variable user (convert UID back to a user)
- Use the second variable for your second user action
See #1169800-40: [Integrity constraint violation: 1062 Duplicate entry] On user registration for a more detailed version.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | add_new_user_to_group.txt | 463 bytes | sarah |
| #5 | add_role_to user.txt | 416 bytes | sarah |
| #5 | add_role_to_new_user.txt | 559 bytes | sarah |
Comments
Comment #1
goodale commentedJust confirming that it is unrelated to adding roles. My scenario is:
1) check if the account has a role
2) check if a node reference field on the account is empty
3) create a new piece of content
4) save that piece of content
5) assign the content to the node reference field
I get
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '117-4' for key 'PRIMARY': INSERT INTO {users_roles} (uid, rid) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1); Array ( [:db_insert_placeholder_0] => 117 [:db_insert_placeholder_1] => 4 ) in user_save() (line 603 of /home/goodale/websites/sfsdevel/modules/user/user.module).
which is interesting as I am not assigning a role.
The same workaround works though; if I change it to
1) check if the account has a role
2) check if a node reference field on the account is empty
3) create a new piece of content
4) save that piece of content
5) Add a variable (convert the user to a UID (integer) variable)
6) Add a variable user (convert UID back to a user)
7) assign the content to the node reference field
the error doesn't appear and everything works as expected.
Comment #2
fagoYep, this is still an issue if you somehow trigger saving the user entity after creation.
Comment #3
fagoThis is a core problem, see #1433288: Integrity constraint violation when saving a user account after creation - thus postponing on that.
Comment #4
tylerfrans commentedIn case this helps at all, I'm getting this issue when adding a user to an organic group after saving a new user account. It works when my user does not have any roles, but throws an integrity constraint error when the new user has a role.
Rules setup:
Event: After saving a new user account
Actions:
Fetch entity by property (get group node by a user data field)
Fetch entity by property (get group by the node)
Subscribe user to group
Error message: PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '82-5' for key 'PRIMARY': INSERT INTO {users_roles} (uid, rid) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1); Array ( [:db_insert_placeholder_0] => 82 [:db_insert_placeholder_1] => 5 ) in user_save() (line 603 of /home/ijm/public_html/modules/user/user.module).
I hope this helps.
Comment #5
sarah commentedI worked around this by setting data values on the new registration (in my case I set the data values to add group membership and make it active)
I added a second rule to run on event "After updating an existing user account", Condition if the user has the Authenticated user role, set the role to whatever (in my case it's called member, with rid 4).
If you need to be more specific in allocating your role to ensure ONLY new accounts are targetted (ie to not affect existing accounts which might have been created already but not have a role outside of authenticated user), add a condition based on account creation date (direct input a value of creation date is greater than now -10 secs). For example, this might be necessary if you're running a promotion where people signing up now get privileges but you have users whose 'trial membership' has expired and defaulted to 'authenticated user'.
I've attached the Rules exports for the above just in case it helps someone (as separate files to keep the clutter down)
Side note: The nice thing about this approach is that it could be modified I think if you are requiring new users to be approved (to allow you to automatically add new users to a group before they're actually approved as an authenticated user, and when they're approved add them to a role by setting the created date to greater than now -1 day (for instance))
Comment #5.0
sarah commentedCTRL+V is a powerful tool indeed
Comment #6
Bojhan commentedSo creating an additional rule circumvents the problem?
Comment #7
Bojhan commentedI am going to open this, none of the patches in #1433288: Integrity constraint violation when saving a user account after creation seem to solve this issue and it looks like people find rules work arounds to solve it which seems to put it more in rules it basket than core.
Comment #8
picacsso commentedI found the solution @ https://drupal.org/node/1433288
Implementing patch #42 fixed the problem.
Comment #9
cromian commentedSo this is a core issue?
Comment #10
afagioli@emenuic, you may want to link to #42 comment itself, right?
https://drupal.org/comment/8312299#comment-8312299
Comment #11
lias commented#42 helped but didn't completely resolve the issue with rules and users. I am also using entity registration and when I set a rule to assign a role to a user it results in the integrity constraint violation and anonymous, authenticated users can't register for events.
I've explained more at entity registration issues: https://www.drupal.org/node/2311263
Comment #12
afagioli