Ruby on rails ActiveRecord::语句无效:PG::UndefinedTable:错误:关系“类别”不存在
我正在构建一个小的rails应用程序。当我运行heroku run rake db:migrate时,我得到这个错误Ruby on rails ActiveRecord::语句无效:PG::UndefinedTable:错误:关系“类别”不存在,ruby-on-rails,postgresql,ruby-on-rails-4,heroku,Ruby On Rails,Postgresql,Ruby On Rails 4,Heroku,我正在构建一个小的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" se
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")
)
为了解决这个问题,我还将其添加到definctions.rb中
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'category', 'categories'
inflect.plural 'category', 'categories'
end
这没用
我还查看了有关stackoverflow的一些答案,包括,但没有帮助,因为我在运行迁移命令时遇到了这个错误
这是我的分类和习惯迁移文件
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
不知道我还错过了什么 似乎您的迁移文件就是问题所在。也许可以试试rakedb:schema:load。我以前也遇到过类似的问题,这总是因为在初始迁移之后添加了一个列 您的迁移似乎有问题。在本地运行此命令,查看它们是否运行正常:rake db:drop&&rake db:create&&rake db:migrate
PS:我没有告诉您运行rake db:reset,因为这样会加载模式而不是运行迁移。最后,我不得不销毁我的Heroku应用程序并重新创建它。解决了这个问题。我的应用程序中没有太多数据。所以,我能做到。如果你有一个非常好的数据库,我不会建议你这么做 要销毁应用程序,heroku应用程序:销毁
要再次创建它,heroku create appname检查添加/修改/删除引用的迁移。嗯,我们可以看看您的模式吗?也不确定,但这可能会有帮助,非常感谢您的时间。我已经编辑了我的问题以包括schema.rbAlright。我最好的猜测仍然是迁移的顺序。这个问题的答案可能是一个解决方案:另一方面,我不想这么说,但是heroku运行rake db:reset或rake db:drop:all,然后重新尝试迁移。如果您有不想销毁的数据,则这些选项不在表中。此外,根据您在模型中进行引用的方式,迁移可能必须是t.references设置。ctrl+f引用第二个:添加\引用:产品,:用户,外键:真的好运气,我希望有东西适合你!
# 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