title: Omniauth error -- PG::Error: ERROR: duplicate key value violates
body: <p>So, I've helped a friend add Omniauth to his app, and for some reason omniauth only worked the first time. </p>
  
  <p>But now, it's returning:</p>
  
  <pre><code>     PG::Error: ERROR: duplicate key value violates 
      unique constraint "index_users_on_email" 
  DETAIL: Key (email)=() already exists. : INSERT INTO "users" 
  ("created_at", "provider", "uid", "updated_at", "username") 
  VALUES ($1, $2, $3, $4, $5) RETURNING "id"
  </code></pre>
  
  <p>It's like it only accepts one, and then says "already exists". How can we get this workin'?</p>
  
  <p><strong>user.rb</strong></p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :token_authenticatable, :confirmable,
    # :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable, :omniauthable,
           :recoverable, :rememberable, :trackable, :validatable
  
    has_many :songs
    has_many :comments
  
    acts_as_voter
  
    def self.from_omniauth(auth)
      where(auth.slice(:provider, :uid)).first_or_create do |user|
        user.provider = auth.provider
        user.uid = auth.uid
        user.username = auth.info.nickname
      end
    end
  
    def self.new_with_session(params, session)
      if session["devise.user_attributes"]
        new session["devise.user_attributes"] do |user|
            user.attributes = params
          user.valid?
        end
      else
        super
      end
    end
  
    def password_required?
      super &amp;&amp; provider.blank?
    end
  
    def update_with_password(params, *options)
      if encrypted_password.blank?
        update_attributes(params, *options)
      else
        super
      end
    end
  
    def email_required?
      super &amp;&amp; provider.blank?
    end
  
  
  end
  </code></pre>
  
  <p><strong>schema snippit</strong></p>
  
  <pre><code> create_table "users", force: true do |t|
      t.string   "email",                  default: "", null: false
      t.string   "encrypted_password",     default: "", null: false
      t.string   "reset_password_token"
      t.datetime "reset_password_sent_at"
      t.datetime "remember_created_at"
      t.integer  "sign_in_count",          default: 0
      t.datetime "current_sign_in_at"
      t.datetime "last_sign_in_at"
      t.string   "current_sign_in_ip"
      t.string   "last_sign_in_ip"
      t.datetime "created_at"
      t.datetime "updated_at"
      t.boolean  "admin"
      t.string   "provider"
      t.string   "uid"
      t.string   "username"
    end
  </code></pre>
  
  <p><strong>omni controller</strong></p>
  
  <pre><code>class OmniauthCallbacksController &lt; Devise::OmniauthCallbacksController
    def all
      user = User.from_omniauth(request.env["omniauth.auth"])
      if user.persisted?
        flash.notice = "Signed in!"
        sign_in_and_redirect user
      else
        session["devise.user_attributes"] = user.attributes
        redirect_to new_user_registration_url
      end
    end
    alias_method :twitter, :all
  end
  </code></pre>
  
  <p>All access keys are setup appropriately following railscasts latest ep on it.</p>