Reputation: 1482
I have a devise user model.When a user is to be created there is no email required (email is optional).I can create the user without email (blank email ) for the first time.When i add a user second time the error shows up
duplicate key value violates unique constraint "index_accounts_on_email"
DETAIL: Key (email)=() already exists.
I think this happen if a blank email already exists.
Is there a way to validate the uniqueness only if the email is not blank and i can add as many user with blank emails?
user.rb
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable, :omniauthable
protected
def password_required?
false
end
def email_required?
false
end
end
Upvotes: 2
Views: 392
Reputation: 1729
If you look in your migration file that created the users you will find the line:
add_index :users, :email, unique: true
One way to remove this is removing the index complete and add it again without uniqueness:
remove_index :users, :email
add_index :users, :email
Now it won't check for email uniqueness and you can handle this by yourself whenever and wherever you want.
Upvotes: 4