2
votes

i keep getting error message: -- add_column(:articles, :user_id, :integer) rake aborted! StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: duplicate column name: user_id: ALTER TABLE "articles" ADD "user_id" integer

here are All the migration i made:

 class AddUserIdToArticles < ActiveRecord::Migration
  def change
    add_column :articles, :user_id, :integer
  end
end

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
        t.string :username
        t.string :email
        t.timestamps
    end
  end
end

class AddDescriptionToArticles < ActiveRecord::Migration
  def change
    add_column :articles, :description, :text
    add_column :articles, :created_at, :datetime
    add_column :articles, :updated_at, :datetime
  end
end

class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles do |t|
      t.string :title
    end
  end
end

1

1 Answers

3
votes

The error is on

class AddUserIdToArticles < ActiveRecord::Migration
  def change
    add_column :articles, :user_id, :integer
  end
end

Because the column user_id already exists on the table articles

You can verify by changing the code to:

class AddUserIdToArticles < ActiveRecord::Migration
  def change
    unless column_exists? :articles, :user_id
      add_column :articles, :user_id, :integer
    end
  end
end

But I don't advice it, the column should not be there at the first place, or you are just trying to add a column that is already there.

BTW, I would advice to add an index to your foreign keys, like user_id