Ruby on rails Rails编辑迁移以忽略错误
我有一个应用程序,上面有帖子、用户、标签等等。我一直在本地进行这项工作,但由于一个问题,无法将其推给heroku。最后,我成功地将我的应用程序推到heroku,然后意识到我从未在那里迁移过我的数据库。所以我跑了Ruby on rails Rails编辑迁移以忽略错误,ruby-on-rails,ruby,heroku,Ruby On Rails,Ruby,Heroku,我有一个应用程序,上面有帖子、用户、标签等等。我一直在本地进行这项工作,但由于一个问题,无法将其推给heroku。最后,我成功地将我的应用程序推到heroku,然后意识到我从未在那里迁移过我的数据库。所以我跑了 heroku run rake db:migrate 得到了这个错误: == 20141116151429 CreatePosts: migrating ====================================== -- drop_table(:posts) PG::U
heroku run rake db:migrate
得到了这个错误:
== 20141116151429 CreatePosts: migrating ======================================
-- drop_table(:posts)
PG::UndefinedTable: ERROR: table "posts" does not exist
: DROP TABLE "posts"
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: table "posts" does not exist
我查阅了迁移,出于某种原因,它在其他任何内容之前有drop tables行:
class CreatePosts < ActiveRecord::Migration
def change
drop_table :posts
create_table :posts do |t|
t.string :title, null: false, default: ""
t.text :description, null: false, default: ""
t.timestamps
end
end
end
我注释掉了drop表行,甚至删除了它,然后使用git commit提交,然后尝试运行heroku-rake-db:migrate,但仍然显示错误。我知道这是一个严重的混乱,但我不知道从这里该怎么办
我没有尝试重新设置数据库,因为担心会出现问题,尽管从技术上讲,我可能会丢失我迄今为止创建的帖子/用户/评论等
有没有一种方法可以补救这个问题,并且仍然将所有数据库数据推送到heroku?如果没有,我首先应该做什么才能让应用程序运行
编辑:
我甚至将迁移更改为:
class CreatePosts < ActiveRecord::Migration
def up
create_table :posts do |t|
t.string :title, null: false, default: ""
t.text :description, null: false, default: ""
t.timestamps
end
end
def down
drop_table :posts
end
end
但错误仍然存在。我不明白:我知道我编辑了正确的文件20141116151429_create_posts.rb,然后删除了那一行。甚至更改了它的全部内容并提交了这些更改,但是运行heroku rake db:migrate仍然会出现drop_tables错误
据我所知,heroku rake db:migrate与rake db:migrate不是非常相似吗?如果是这样,那么它应该运行挂起的迁移。在这种情况下,他们都是,因为我从来没有迁移到heroku。如果是这样的话,那么我对迁移文件所做的任何尚未运行的更改都应该得到反映。然而我却犯了这个错误 当您注释掉drop_表语法时,它应该会起作用。但如果它不起作用,那么尝试使用下面的语法 只有当数据库中存在表时,它才会运行drop_table
class CreatePosts < ActiveRecord::Migration
def change
drop_table 'posts' if ActiveRecord::Base.connection.table_exists? 'posts'
create_table :posts do |t|
t.string :title, null: false, default: ""
t.text :description, null: false, default: ""
t.timestamps
end
end
end
这真让人难堪
感谢@Dipak和@RubyOnRails的快速回复
在第一次提交之后,我的提交似乎完全忽略了迁移文件。我返回并使用添加的更改进行了干净的提交,迁移过程中没有出现任何问题
也许我对希罗库的承诺在第一次之后没有实现
尴尬。如果您使用“更改”,则删除行是无用的
# == Reversible Migrations
#
# Starting with Rails 3.1, you will be able to define reversible migrations.
# Reversible migrations are migrations that know how to go +down+ for you.
# You simply supply the +up+ logic, and the Migration system will figure out
# how to execute the down commands for you.
#
# To define a reversible migration, define the +change+ method in your
# migration like this:
#
# class TenderloveMigration < ActiveRecord::Migration
# def change
# create_table(:horses) do |t|
# t.column :content, :text
# t.column :remind_at, :datetime
# end
# end
# end
#
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title, null: false, default: ""
t.text :description, null: false, default: ""
t.timestamps
end
end
end
你不能做def down和drop_table.Comment drop_table:posts和migrate,它会工作的…我做了注释掉drop_table-似乎是最合乎逻辑的反应。我甚至删除了那一行并运行了迁移,但错误仍然显示。好的,将def name重新命名为def up和comment drop_table:posts,现在试试……我尝试将整个结构更改为上面的编辑-它仍然显示-drop_table:posts。。。我只是很震惊,为什么当我把它移走的时候,它坚持说那条线在那里。有什么想法吗?顺便说一句,谢谢你的快速回复。