【问题标题】:ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "categories" does not existActiveRecord::StatementInvalid: PG::UndefinedTable: 错误: 关系“类别”不存在
【发布时间】:2017-06-13 17:23:35
【问题描述】:

我正在构建一个小型 Rails 应用程序。当我运行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

不知道我还缺少什么!

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 检查您添加/修改/删除引用的迁移。
    【解决方案2】:

    您的迁移文件似乎是问题所在。也许尝试 rake db:schema:load 代替。我之前也遇到过类似的问题,这总是因为在初始迁移后添加了一个列。

    【讨论】:

    • 嗯,我们可以看看你的架构吗?也不确定,但这可能会有所帮助stackoverflow.com/questions/5450930/…
    • 非常感谢您的宝贵时间。我已经编辑了我的问题以包含 schema.rb
    • 好吧,我最好的猜测仍然是执行迁移的顺序。对此的检查答案可能是一种解决方案:stackoverflow.com/questions/30290250/… 否则,我不想这么说,但是 heroku 运行 rake db:reset 或 rake db:drop:all 然后重新尝试迁移。如果您有不想销毁的数据,则这些选项不可用,但请注意。
    • 另外,根据您在模型中进行引用的方式,迁移可能必须是 t.references 设置。 edgeguides.rubyonrails.org/active_record_migrations.html ctrl+f 引用第二个: add_reference :products, :user, foreign_key: true 祝你好运,希望对你有用!
    【解决方案3】:

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

    要销毁应用,heroku apps:destroy

    再创建一次,heroku create appname

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-14
      • 2022-09-24
      • 1970-01-01
      相关资源
      最近更新 更多