2

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 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

@MikeShasaco if my answer helped you, feel free to give me a vote and mark my answer as accepted, thats the way stackoverflow works ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.