Heroku rake 迁移数据库错误关系"post"不存在



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

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

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

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

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

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

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

然后用第一个命令重新检查迁移状态,并确保所有迁移状态都列为"向下"。这将删除所有表等。然后进入迁移文件,从给您带来问题的Comments表中删除对posts表的引用。然后重新运行所有迁移。然后通过生成并运行以下迁移,将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!

最新更新