user273072545345
user273072545345

Reputation: 1566

issues with installing devise

here's my current user schema:

# == Schema Information
#
# Table name: users
#
#  id         :integer          not null, primary key
#  first_name :string
#  last_name  :string
#  created_at :datetime         not null
#  updated_at :datetime         not null
#
require 'elasticsearch/model'

class User < ActiveRecord::Base
    searchkick word_start: [:user]
    has_many :posts
    validates :first_name, :last_name, presence: true
end

these are the steps I'm able to go through before the problem rears its head:

rails generate devise:install     
rails generate devise user        
rake db:migrate  

once i try to migrate it, this is what comes up:

== 20160707230510 AddDeviseToUsers: migrating =================================
-- change_table(:users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::DuplicateColumn: ERROR:  column "email" of relation "users" already exists
: ALTER TABLE "users" ADD "email" character varying DEFAULT '' NOT NULL
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:5:in `block in up'
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:3:in `up'
ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR:  column "email" of relation "users" already exists
: ALTER TABLE "users" ADD "email" character varying DEFAULT '' NOT NULL
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:5:in `block in up'
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:3:in `up'
PG::DuplicateColumn: ERROR:  column "email" of relation "users" already exists
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:5:in `block in up'
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:3:in `up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

as you can tell from my earlier user schema, there is no email column there. so ... why does this error come up?

**EDIT - should have posted devise migration file - the one that doesn't work **

class AddDeviseToUsers < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.inet     :current_sign_in_ip
      t.inet     :last_sign_in_ip


      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      # Uncomment below if timestamps were not included in your original model.
      # t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end

  def self.down
    # By default, we don't want to make any assumption about how to roll back a migration when your
    # model already existed. Please edit below which fields you would like to remove in this migration.
    raise ActiveRecord::IrreversibleMigration
  end
end

EDIT AGAIN

when i run rails g devise user ... this is what comes up:

Running via Spring preloader in process 72318
      invoke  active_record
      create    db/migrate/20160707230510_add_devise_to_users.rb
      insert    app/models/user.rb
       route  devise_for :users

Upvotes: 0

Views: 456

Answers (2)

user273072545345
user273072545345

Reputation: 1566

Turns out all I had to do was to do rake db:setup which recreates the db. Then I ran rake db:migrate, and no issues came up this time.

FYI. Hope this helps someone out there.

Upvotes: 1

Khanh Pham
Khanh Pham

Reputation: 2973

To eliminate migration errors on duplicate fields, use t.change as shown below.

t.change :email, :string,     :null => false, :default => "" 

Notice that for t.change to work, you have to specify the type for the field being changed. In the case of the email migration above, the email field was of type string.

You can refer at: devise wiki

Upvotes: 0

Related Questions