【问题标题】:heroku rake migrate db error relation "post" doesn't existheroku rake migrate db 错误关系“post”不存在
【发布时间】:2016-09-10 20:34:58
【问题描述】:

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

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

希望有人可以帮助我...感谢您所做的一切!

【问题讨论】:

  • 是否需要保留数据库中已有的数据?
  • 是的。如果可能的话……

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


【解决方案1】:

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

我会手动创建一个新的迁移文件,而不是使用 rails 生成器,并给它一个比 Comments 表迁移文件的时间戳早几秒钟的时间戳。然后,我将在该迁移中创建 Posts 表并编辑创建 Posts 表的其他现有迁移以添加它的列。然后,您必须进入您的数据库(不是您的 rails 控制台,而是您的数据库,可能是 Postgres)并将此迁移插入到“schema_migrations”表中。每当您生成迁移并运行 db:migrate 时,rails 都会自动设置此 schema_migrations 表。您会注意到此 schema_migrations 表中的条目按“版本”列出,其中版本是迁移文件夹中迁移的时间戳。然后,您必须将迁移文件时间戳添加到您的 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”。这将删除所有表等。然后进入您的迁移文件并从给您问题的 Comments 表中删除对您的帖子表的引用。然后再次重新运行所有迁移。然后通过生成并运行以下迁移将您的 posts_id 引用添加到 cmets 表,如下所示:

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!

【讨论】:

  • 我试过了,但我不能.....还有其他方法吗?在这一点上,我真的不在乎丢失数据。
  • 如果您不在乎丢失数据,我会用更简单的方式更新答案,哈哈
  • 谢谢你!你真棒!在执行 rake db:migrate 之前,我忘记使用 git 进行 sinc。现在我收到另一个错误,但我传递了所有数据。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2012-09-22
  • 2012-06-16
  • 2012-12-09
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 2011-06-05
  • 1970-01-01
相关资源
最近更新 更多