heroku rake migrate db error relation“post”不存在

时间:2016-05-14 18:35:19

标签: ruby-on-rails ruby database heroku migrate

所以..我第一次尝试将我的数据库迁移到heroku,执行:

heroku rake db:migrate

但我得到了这个我无法解决的可怕错误!

    ** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
  ActiveRecord::SchemaMigration Load (1.8ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to CreateComments (20150528110329)
   (1.6ms)  BEGIN
== 20150528110329 CreateComments: migrating ===================================
-- create_table(:comments)
   (9.4ms)  CREATE TABLE "comments" ("id" serial primary key, "name" character varying, "body" character varying, "post_id" integer, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
   (3.8ms)  CREATE  INDEX  "index_comments_on_post_id" ON "comments"  ("post_id")
   (6.4ms)  ALTER TABLE "comments" ADD CONSTRAINT "fk_rails_2fd19c0db7"
FOREIGN KEY ("post_id")
  REFERENCES "posts" ("id")

PG::UndefinedTable: ERROR:  relation "posts" does not exist
: ALTER TABLE "comments" ADD CONSTRAINT "fk_rails_2fd19c0db7"
FOREIGN KEY ("post_id")
  REFERENCES "posts" ("id")

   (1.6ms)  ROLLBACK
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

我能理解的是它试图引用一个尚不存在的表...但我不知道如何首先创建我的帖子表...

schema.rb>

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

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

  add_index "comments", ["post_id"], name: "index_comments_on_post_id"

  create_table "posts", force: :cascade do |t|
    t.string   "title"
    t.text     "subtitle"
    t.text     "content"
    t.string   "img"
    t.string   "category"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "taggings", force: :cascade do |t|
    t.integer  "tag_id"
    t.integer  "taggable_id"
    t.string   "taggable_type"
    t.integer  "tagger_id"
    t.string   "tagger_type"
    t.string   "context",       limit: 128
    t.datetime "created_at"
  end

  add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
  add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"

  create_table "tags", force: :cascade do |t|
    t.string  "name"
    t.integer "taggings_count", default: 0
  end

  add_index "tags", ["name"], name: "index_tags_on_name", unique: true

end

我尝试尽可能切换我的代码但没有结果。

希望有人能帮助我......谢谢你的一切!

1 个答案:

答案 0 :(得分:1)

我认为你的某个迁移的某个地方已被编辑(如果没有正确回滚,那将是问题等)并且现在导致了一个重大问题。而且由于你需要保留数据并且无法完成db:rollback我会说除非别人有更好的想法,否则我可以考虑一个选项。我从不建议编辑迁移文件,但在某些情况下我过去必须这样做。执行以下操作并确保记住,对数据库的任何更改都无法通过简单的方法撤消 git checkout .

我会手动创建一个新的迁移文件,而不是使用rails生成器,并为其提供一个时间戳,该时间戳是在注释表迁移文件的时间戳之前的几秒钟。然后,我将在该迁移中创建Posts表,并编辑创建Posts表的其他现有迁移,只需添加它的列。然后,您必须进入数据库(不是您的rails控制台,而是您的数据库,可能是Postgres)并将此迁移插入“schema_migrations”表。每当您生成迁移并运行db:migrate时,都会通过rails自动设置此schema_migrations表。您会注意到此schema_migrations表中的条目按“Version”列出,其中version是迁移文件夹中迁移的时间戳。然后,您必须将迁移文件时间戳添加到schema_migrations表中,这应该是好的。

更新:如果您不关心丢失数据,则可以回滚所有迁移,编辑迁移文件,然后重新运行它们。

bundle exec rake db:migrate:status #Look at the first version id and copy it.

bundle exec rake db:rollback VERSION=theVersionNumberYouCopiedHere

然后使用第一个命令重新检查迁移状态,并确保所有迁移状态都列为“down”。这将删除所有表等。然后进入您的迁移文件,并从提供问题的评论表中删除您的posts表的引用。然后再次重新运行所有迁移。然后通过生成并运行以下迁移,将posts_id引用添加到comments表中:

rails g migration AddPostToComments post:references

查看该迁移文件,您应该看到如下内容:

class AddPostToCommentss < ActiveRecord::Migration
  def change
    add_reference :comments, :post, index: true
    add_foreign_key :comments, :posts
  end
end

现在运行bundle exec rake db:migrate你应该好好去。将所有更改提交给git,然后推送到heroku!