Heroku的耙DB迁移错误关系“后”不存在
所以..我尝试我的数据库迁移到Heroku的首次,这样做的:Heroku的耙DB迁移错误关系“后”不存在
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 .
我将手动创建一个新的迁移文件被撤销,而不是使用轨道发电机,并给它在注释表迁移文件的时间戳之前几秒钟的时间戳。然后,我将在该迁移中创建Posts表格,并编辑另一个创建Posts表格的现有迁移以添加它的列。然后您必须进入数据库(不是您的rails控制台,而是可能是Postgres的数据库)并将此迁移插入到“schema_migrations”表中。当您生成迁移并运行db:migrate时,这个schema_migrations表由rails自动设置。您会注意到此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
然后重新检查迁移状态与第一个命令,确保所有迁移状态被列为“下”。这将删除所有表格等。然后进入你的迁移文件,并从评论表中删除引用你的帖子表,这是给你的问题。然后再次重新运行所有迁移。然后添加你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!
我试过了,但是我不知道......还有其他方法吗?在这一点上,我真的不在乎丢失数据。 –
如果你不在乎丢失数据,我会以更简单的方式更新答案lol – bkunzi01
谢谢你!你太棒了!在执行rake db之前,我忘记了用git来配合:迁移。现在即时通讯还有其他错误,但我传递了所有数据。再次感谢。 –
您是否需要保留数据库中的数据? – bkunzi01
是的。如果可以的话...... –