Emma O.
Emma O.

Reputation: 25

I have a duplicate column name error using MySql when trying to deploy Rails app

ActiveRecord::StatementInvalid: Mysql2::Error: Duplicate column name 'first_name': ALTER TABLE `users` ADD `first_name` varchar(255)

This is the error I am receiving when I am trying to deploy my Rails app using ShipIt.

My question is: how do I resolve this error?

I started off my application with the user having an attribute called name, and then split up name into first_name and last_name in the following migration. When I reset my database (rails db:reset) everything works fine. When I drop my database, create it, migrate my changes, and seed manually everything looks dandy. My schema looks like it contains only the data that I want. I am confused. When I query the tables using sql and rails, the columns look fine.

 /app/db/migrate/20180507080705_break_up_fullname_in_users.rb:3:in `up'

That is where it says the error is caused.

This is the entire migration which seems to cause the error:

class BreakUpFullnameInUsers < ActiveRecord::Migration[5.2]
  def up
  add_column :users, :first_name, :string
  add_column :users, :last_name, :string

  User.all.each do |user|
    fn, ln = name.split(' ', 2)
    user.update(first_name: fn, last_name: ln)
  end

  remove_column :users, :name
 end


  def down
    add_column :users, :name, :string

   User.all.each do |user|
     n = [name.first_name, name.last_name].join(' ')
     user.update(name: n)
   end

    remove_column :users, :first_name
    remove_column :users, :last_name
  end

end

And this was the original migration to create the table:

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

   t.timestamps
  end
 end
end

This is my in my schema file under users:

create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
 t.string "email"
 t.string "username"
 t.string "password_digest"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
 t.string "first_name"
 t.string "last_name"
 t.boolean "admin", default: false
end

Upvotes: 0

Views: 3208

Answers (1)

Satishakumar Awati
Satishakumar Awati

Reputation: 3790

The error is coming because the columns first_name and last_name already been added/committed to the table in database. And also name column from users table is removed.

The migration execution already completed and somehow rails has failed to update migration version in schema_migrations table might be due to the code that is updating data to two columns first_name and last_name from name field.

So I would suggest comment the all lines present in up method and re deploy the file to the server => run the migrations and start the server.

def up
  #add_column :users, :first_name, :string
  #add_column :users, :last_name, :string

  #User.all.each do |user|
   # fn, ln = name.split(' ', 2)
   #user.update(first_name: fn, last_name: ln)
  #end

  #remove_column :users, :name
 end

Upvotes: 1

Related Questions