devise_invitable icon indicating copy to clipboard operation
devise_invitable copied to clipboard

ActiveRecored::RecordNotUnique in bulk `invite!`

Open toby-brilliant opened this issue 9 months ago • 3 comments

Potentially this is a regression in this or a related gem?

Working on functionality around uploading a CSV and invite!-ing users I'm seeing:

ActiveRecord::RecordNotUnique Mysql2::Error: Duplicate entry '129332' for key 'users.PRIMARY'

The code effectively looks like:

User.invite!({ email: email, name: name }, inviting_user) do |u|
  u.skip_invitation = true
  u.skip_confirmation_notification!
end

For ~1600 users in a loop, though it seems to hit the duplicate almost immediately. Creating the users beforehand and then inviting them separately appears to work around this issue.

Environment

DeviseInvitable 2.0.10 Devise 4.9.4 MySQL 8.0.40 Rails 7.0.8.7 Ruby 3.3.5

toby-brilliant avatar Mar 28 '25 17:03 toby-brilliant

Potentially this is a regression in this or a related gem?

Working on functionality around uploading a CSV and invite!-ing users I'm seeing:

ActiveRecord::RecordNotUnique Mysql2::Error: Duplicate entry '129332' for key 'users.PRIMARY'

The code effectively looks like:

User.invite!({ email: email, name: name }, inviting_user) do |u|
  u.skip_invitation = true
  u.skip_confirmation_notification!
end

For ~1600 users in a loop, though it seems to hit the duplicate almost immediately. Creating the users beforehand and then inviting them separately appears to work around this issue.

Environment

DeviseInvitable 2.0.10 Devise 4.9.4 MySQL 8.0.40 Rails 7.0.8.7 Ruby 3.3.5

Make sure config.validate_on_invite = true is enabled. However the drawback is that you will be forced to provide a password when inviting record. This is weirdly done.

Edit

Analyzing the source code, it seems to simply be an inconsistent behavior

User.new(**params).invite! -> requires password
User.invite!(**params) -> doesn't require password, it assigns a random one automatically

jamesst20 avatar Apr 04 '25 22:04 jamesst20

Make sure config.validate_on_invite = true is enabled

I'm not sure I'm following - to what end? Are you saying something about that configuration would fix the primary key values?

toby-brilliant avatar Apr 08 '25 01:04 toby-brilliant

Make sure config.validate_on_invite = true is enabled

I'm not sure I'm following - to what end? Are you saying something about that configuration would fix the primary key values?

The other day I though the exception was caused by an SQL unique index that was triggered. This usually happens when valuations are skipped. However reading the exception this morning, I realize this is not what is happening, sorry about that.

jamesst20 avatar Apr 08 '25 11:04 jamesst20