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

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

你能帮我弄清楚我还需要编辑或更改什么吗? 我有 Task App 并且有 3 个模型 Category、User 和 Task 它们已经相关了。如果您想在此处查看我的模型,该应用程序在我的本地机器上运行良好:

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

是的!这就是我的模型的样子。

我能够在 heroku 上 deoply 我的应用程序,然后我 运行 这个:

 heroku run rake db:migrate

并收到此错误消息:

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

我检查了我的 schema.rb 和 user_id Uder 任务 table 在那里。

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```



我已经解决了。

我刚刚删除了导致错误的文件,我创建了一个迁移文件来从任务 table 中删除 user_id。 Rails 假设 user_id 列由于迁移文件而被删除或不存在。