ActiveRecord :: StatementInvalid:PG :: UndefinedTable:ERROR:关系“categories”不存在

时间:2017-06-13 17:23:35

标签: ruby-on-rails postgresql ruby-on-rails-4 heroku

我正在构建一个小型应用程序。何时,我运行heroku run rake db:migrate,我收到此错误

rake aborted!

StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "categories" does not exist
: CREATE TABLE "habits" ("id" serial primary key, "name" character varying, "description" character varying, "category_id" integer, "user_id" integer, "created_at
" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_23642321ab"
FOREIGN KEY ("category_id")
  REFERENCES "categories" ("id")
, CONSTRAINT "fk_rails_541267aaf9"
FOREIGN KEY ("user_id")
  REFERENCES "users" ("id")
)

为了解决这个问题,我还将其添加到inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'category', 'categories'

  inflect.plural 'category', 'categories'
end

哪个没用。

我还在stackoverflow上查看了几个答案,包括this,但它没有帮助,因为我在运行迁移命令时收到此错误。

以下是我的类别和习惯的迁移文件。

class CreateCategories < ActiveRecord::Migration[5.0]
  def change
    create_table :categories do |t|
      t.string :name

      t.timestamps
    end
  end
end

class CreateHabits < ActiveRecord::Migration[5.0]
  def change
    create_table :habits do |t|
      t.string :name
      t.string :description

      t.integer :user_id
      t.integer :category_id

      t.timestamps
    end
  end
end

这是我的schema.rb

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170612231416) do

  create_table "categories", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "comments", force: :cascade do |t|
    t.text     "description"
    t.integer  "habit_id"
    t.integer  "user_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "goals", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "goals_habits", force: :cascade do |t|
    t.integer  "habit_id"
    t.integer  "goal_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "goals_milestones", force: :cascade do |t|
    t.integer  "goal_id"
    t.integer  "milestone_id"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
  end

  create_table "habits", force: :cascade do |t|
    t.string   "name"
    t.string   "description"
    t.integer  "user_id"
    t.integer  "category_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "milestones", force: :cascade do |t|
    t.string   "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "milestones_statuses", force: :cascade do |t|
    t.integer  "milestone_id"
    t.integer  "status_id"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
  end

  create_table "statuses", force: :cascade do |t|
    t.string   "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "email"
    t.string   "password_digest"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.integer  "role"
  end

end

不确定我还缺少什么!

3 个答案:

答案 0 :(得分:1)

您的迁移似乎存在问题。在本地运行以查看它们是否正常运行:rake db:drop && rake db:create && rake db:migrate

PS:我没有告诉你运行rake db:reset,因为它会加载架构而不是运行迁移。

答案 1 :(得分:0)

您的迁移文件似乎有问题。也许尝试rake db:schema:加载。我以前遇到过类似的问题,并且总是因为在初始迁移后添加了一列。

答案 2 :(得分:0)

最后,我只需要销毁我的Heroku应用程序并重新创建它。这解决了这个问题。我的应用中没有太多数据。所以,我可以做到。如果你有一个非常好的数据库,我不会建议它。

要销毁应用,heroku apps:destroy

再次创建它,heroku create appname