RoR:无法将数据库迁移到Heroku

时间:2016-05-30 12:08:49

标签: ruby-on-rails heroku

我无法将数据库迁移到Heroku。我已经检查了解决这个问题的其他问题无济于事。我真的可以在我的代码上使用第二双眼睛来帮助我解决这个问题。

这是我得到的错误:

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

PG::UndefinedTable: ERROR:  relation "props" does not exist
: ALTER TABLE "comments" ADD CONSTRAINT "fk_rails_1d3f70cf04"
FOREIGN KEY ("prop_id")
  REFERENCES "props" ("id")

enter image description here

迁移此文件时似乎被抓住了:

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.string :commenter
      t.text :body
      t.references :prop, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

这是我创建表道具的迁移文件:

class CreateProps < ActiveRecord::Migration
  def change
    create_table :props do |t|
      t.string :title
      t.text :text
      t.references :user, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

我的架构在这里:

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

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "answers", force: :cascade do |t|
    t.string   "choice"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string   "created_by"
    t.integer  "user_id"
    t.integer  "prop_id"
  end

  create_table "comments", force: :cascade do |t|
    t.string   "commenter"
    t.text     "body"
    t.integer  "prop_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "comments", ["prop_id"], name: "index_comments_on_prop_id", using: :btree

  create_table "props", force: :cascade do |t|
    t.string   "title"
    t.text     "text"
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string   "choice"
    t.string   "answer"
    t.integer  "answerId"
  end

  add_index "props", ["user_id"], name: "index_props_on_user_id", using: :btree

  create_table "user_answers", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "answer_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: :cascade do |t|
    t.string   "username"
    t.string   "email"
    t.integer  "score",           default: 0
    t.integer  "prop_id"
    t.datetime "created_at",                      null: false
    t.datetime "updated_at",                      null: false
    t.string   "password_digest"
    t.string   "created_by"
    t.boolean  "admin",           default: false
    t.integer  "answers_id"
    t.integer  "answer_id"
  end

  add_index "users", ["answer_id"], name: "index_users_on_answer_id", using: :btree
  add_index "users", ["prop_id"], name: "index_users_on_prop_id", using: :btree

  create_table "wins", force: :cascade do |t|
    t.string   "correctAnswer"
    t.integer  "user_id"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
  end

  add_index "wins", ["user_id"], name: "index_wins_on_user_id", using: :btree

  add_foreign_key "users", "answers"
end

1 个答案:

答案 0 :(得分:1)

问题是您正在创建对尚未创建的表的引用。从迁移中移除引用到props,然后添加props表,然后添加实现关联的迁移。如果您不需要db中当前的数据,我会执行“rake db:drop”并编辑迁移文件(仅当您不与其他人合作时!)

更新:

执行rails g migration add_ref_to_comments

然后编辑迁移以具有:

改变了 add_reference:props,:comment,index:true 端