Search code examples
ruby-on-railsrubydevisedatabase-migration

Rails database migration fails with "duplicate column name: email" when adding devise's generated User table


I am in the process of installing devise. I followed all the required steps and ended here:

$ rails generate devise User
$ rake db:migrate

When I run rake db:migrate I get the following error:

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

SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "em
ail" varchar(255) DEFAULT '' NOT NULLc:/appname/db/migrate/20140618020442_add_d
evise_to_users.rb:5:in `block in up'
c:/appname/db/migrate/20140618020442_add_devise_to_users.rb:3:in `up'
c:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

My project has the following database schema. Some of the devise migration appears to be successful, but the migration was not completed.

ActiveRecord::Schema.define(version: 20140618020442) do

  create_table "listings", force: true do |t|
    t.string   "name"
    t.text     "description"
    t.decimal  "price"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
  end

  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,  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"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

What is the problem and how do I fix it?


Solution

  • So the problem is you already have the users table inside your database. You need to either drop that table and create a new one if it's not working or just continue using that table.

    It's not a good idea to drop your database but since you are making a new app(as you creating a users table) so in your case it'll be better to drop that database and recreate it(to make sure everything works). Try these commands in sequence:

    rake db:drop
    rake db:create
    rake db:migrate
    

    It should work fine and you need not do rails generate devise User as you already have your users migration file

    Update:

    If you don't want to drop your database then you can create a new migration file and delete your users table there.

    rails generate migration DropUsersTable
    

    after that edit your migration file

    class DropUsersTable < ActiveRecord::Migration
      def change
        drop_table :users
      end
    end
    

    and then do rake db:migrate