Search code examples
ruby-on-railsherokudatabase-schemaheroku-postgres

I run heroku run rake db:migrate but I got error PG::UndefinedColumn: ERROR: column "user_id" of relation "tasks" does not exist


Can you help me figure out what else do I need to edit or change here? I have Task App and has 3 models Category, User and Task they are related already. The app is working fine in my local machine if you want to see my models here:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  has_many :tasks
  has_many :categories
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
end
class Task < ApplicationRecord
    belongs_to :category
    belongs_to :user
    

    validates :title, presence: true,
                    length: { minimum: 3 }
    validates :details, length: { minimum: 5 }
end
class Category < ApplicationRecord
    has_many :tasks, :dependent => :destroy
    belongs_to :user
    validates :name, presence: true, uniqueness: true
end

Yes! That's how my model looks like.

I was able to deoply my app on heroku and then I run this:

 heroku run rake db:migrate

and got this error message :

 StandardError: An error has occurred, this and all later migrations canceled:
 PG::UndefinedColumn: ERROR:  column "user_id" of relation "tasks" does not exist

I checked my schema.rb and user_id uder tasks table is there.

ActiveRecord::Schema.define(version: 2020_12_09_004213) do

  create_table "categories", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "user_id"
  end

  create_table "tasks", force: :cascade do |t|
    t.string "title"
    t.text "details"
    t.integer "category_id"
    t.date "set_date"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "user_id"
  end

  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", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end```




Solution

  • I already solved this.

    I just deleted the file that is causing the error, there's a migration file I created to remove the user_id from task table. Rails assuming that the user_id clumn as been removed or not existing because of the migration file.