heroku rake migrate db 错误关系 "post" 不存在

heroku rake migrate db error relation "post" doesn't exist

所以..我第一次尝试将我的数据库迁移到 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:

我能理解的是,它试图从尚不存在的 table 进行引用。但我不知道如何先创建我的 post table...

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

我尝试以各种可能的方式切换我的代码,但没有结果。

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

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

我会手动创建一个新的迁移文件,而不是使用 rails 生成器,并给它一个时间戳,该时间戳比 Comments table 迁移文件的时间戳早几秒。然后我会在该迁移中创建 Posts table 并编辑创建 Posts table 的其他现有迁移以仅添加它的列。然后您必须进入您的数据库(不是您的 rails 控制台,而是您的数据库,可能是 Postgres)并将此迁移插入 "schema_migrations" table。每当您生成迁移和 运行 db:migrate 时,rails 会自动设置此 schema_migrations table。您会注意到此 schema_migrations table 中的条目由 "Version" 列出,其中版本是迁移文件夹中迁移的时间戳。然后,您必须将迁移文件时间戳添加到 schema_migrations table 中,这样就可以了。

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

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

bundle exec rake db:rollback VERSION=theVersionNumberYouCopiedHere

然后用第一个命令重新检查迁移状态,确保所有迁移状态都列为"down"。这将删除所有 table 等。然后进入您的迁移文件并从给您带来问题的评论 table 中删除对您的帖子 table 的引用。然后重新运行 所有迁移。然后通过生成和 运行ning 以下迁移,如下添加对注释 table 的 posts_id 引用:

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!