2017-06-13 76 views
0

我正在构建一个小型Rails应用程序。当我运行heroku run rake db:migrate,我得到这个错误ActiveRecord :: StatementInvalid:PG :: UndefinedTable:错误:关系“categories”不存在

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 

不知道更多,我错过了什么!

回答

0

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

要销毁的应用程序,heroku apps:destroy

而要重新创建,heroku create appname

0

这似乎是你的迁移文件的问题。也许试试rake db:schema:load来代替。之前我有类似的问题,并且始终是因为在初始迁移后添加了一列。

+0

非常感谢。试过它获得相同的错误! –

+0

我们可以看到你的模式吗? 也不知道,但这可能有助于https://stackoverflow.com/questions/5450930/heroku-postgres-error-pgerror-error-relation-organizations-does-not-exist?rq=1 – ekr990011

+0

非常感谢您的时间。我已经编辑我的问题,包括schema.rb –

1

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

PS:我不告诉您运行rake db:reset,因为它会加载模式而不是运行迁移。

+0

非常感谢。试过它获得相同的错误! –

+0

检查添加/修改/删除引用的迁移。 –

相关问题