2017-06-13 60 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

檢查添加/修改/刪除引用的遷移。 –

相關問題