-1

I have seen this question posted several times and the solution is always to drop the database and recreate it. I have data in my database and hence do not want to do that.

Schema:

create_table "users", force: :cascade 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.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "product_id"

  end

My second to last migration file:

class AddProductIdToUsers < ActiveRecord::Migration[5.2]
  def change
    add_column :users, :product_id, :string
  end
end

I have no other migration file that creates a product_id column on my current branch.

I have multiple branches with different database schema. I am wondering if that caused the issue. The branch that might have created the product_id is only there for reference now. It will not be merged to master.

How do I fix this issue? I have tried:

rake db:rollback step=3  
rake db:migrate

but that did not work.

double-beep
  • 5,031
  • 17
  • 33
  • 41
user2012677
  • 5,465
  • 6
  • 51
  • 113

1 Answers1

0

Your create_table is already creating product_id inside the database.

create_table "users", force: :cascade 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.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "product_id" // <- note this line

  end

And you are trying to add another column of same name in your table, which raises an error.

iconoclast
  • 21,213
  • 15
  • 102
  • 138
asimhashmi
  • 4,258
  • 1
  • 14
  • 34