Search code examples
ruby-on-railssqlitedatabase-migration

Rails db migrate aborted error when changing a field name after 1st migrate


I had named the add_column :users, :confirmed_at(confirmation_at), :datetime by accident. So I changed it to the appropriate field name :(confirmed_at)

then I entered : rake db:rollback in command line. it was aborted for some reason with the message below however first my Rails db migrate file code:

class AddConfirmableToDevise < ActiveRecord::Migration
  def up
    add_column :users, :confirmation_token, :string
    add_column :users, :confirmed_at, :datetime
    add_column :users, :confirmation_sent_at, :datetime

    add_index :users, :confirmation_token, unique: true
  end

  def down
    remove_column :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
  end
end

Error after migration:

tzurch:~/workspace (gravitar) $ rake db:migrate:redo

#the error that came up below
_________________________________________________
== 20160902201448 AddFullnameToUser: reverting ================================

-- remove_column(:users, :fullname, :string)

-> 0.0207s

== 20160902201448 AddFullnameToUser: reverted (0.0284s) =======================

== 20160902201448 AddFullnameToUser: migrating ================================

-- add_column(:users, :fullname, :string)

-> 0.0008s

== 20160902201448 AddFullnameToUser: migrated (0.0009s) =======================

== 20160913221959 AddConfirmableToDevise: migrating ===========================

-- add_column(:users, :confirmation_token, :string)

-> 0.0009s

-- add_column(:users, :confirmed_at, :datetime)

-> 0.0005s

-- add_column(:users, :confirmation_sent_at, :datetime)

rake aborted!

StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: duplicate column name: confirmation_sent_at: ALTER TABLE "users" ADD "confirmation_sent_at" datetime:

Solution

  • The problem here is the syntax you are using to remove column is wrong.

    Correct syntax is remove_column

    remove_column(table_name, column_name, type = nil, options = {})
    

    Change down migration with

    class AddConfirmableToDevise < ActiveRecord::Migration
      def up
        add_column :users, :confirmation_token, :string
        add_column :users, :confirmed_at, :datetime
        add_column :users, :confirmation_sent_at, :datetime
    
        add_index :users, :confirmation_token, unique: true
      end
    
      def down
        remove_column :users, :confirmation_token
        remove_column :users, :confirmed_at
        remove_column :users, :confirmation_sent_at
    
        remove_index :users, :confirmation_token
      end
    end
    

    Or remove the down migration altogether and replace up with change, rails will figure out how to rollback:

    class AddConfirmableToDevise < ActiveRecord::Migration
      def change
        add_column :users, :confirmation_token, :string
        add_column :users, :confirmed_at, :datetime
        add_column :users, :confirmation_sent_at, :datetime
    
        add_index :users, :confirmation_token, unique: true
      end
    end