Minh Tâm Lương
Minh Tâm Lương

Reputation: 45

Error when using gem 'devise' for an already existing model

I just tried to add gem 'devise' and met some errors.

But before that I created a model User like this:

    class CreateUsers < ActiveRecord::Migration[5.2]
    def change
    create_table :users do |t|
        t.string :name
        t.string :email
        t.integer :role
        t.string :password_digest
        t.timestamps
    end
    end
    end

Then I ran command 'rails generate devise User' and it created a migrate add_devise_to_user:

    class AddDeviseToUsers < ActiveRecord::Migration[5.2]
    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.string   :current_sign_in_ip
    t.string   :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

Whenever I try to run rails db:migrate , the terminal show me these errors

    == 20190331081250 AddDeviseToUsers: migrating =================================

-- change_table(:users) rails aborted! StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Duplicate column name 'email': ALTER TABLE users ADD email varchar(255) DEFAULT '' NOT NULL

Caused by: ActiveRecord::StatementInvalid: Mysql2::Error: Duplicate column name 'email': ALTER TABLE users ADD email varchar(255) DEFAULT '' NOT NULL

I have checked the questions and solutions that people have given, but none of them helped me solve this problem ( I'm new here )

Upvotes: 1

Views: 280

Answers (1)

QNester
QNester

Reputation: 471

Block in change_table add columns to existed table users. In change_table you trying to add column email, but this column already exists in table users. This column was created in your first migration.

You should comment follow string in devise migration:

 t.string :email,              null: false, default: ""

And add follow string in devise migration after change_table (NOT IN BLOCK) method:

change_column :users, :email, :string, null: false, default: ""

Upvotes: 1

Related Questions