Ruby-on-rails Rails:如何删除待处理的迁移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31843363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: How to delete a pending migration
提问by Leslie Tate
I'm currently following the ruby on rails tutorial: http://guides.rubyonrails.org/getting_started.html.
我目前正在关注 ruby on rails 教程:http: //guides.rubyonrails.org/getting_started.html。
I am trying to save data into the database. However, when I run: rails serverI get the following error:
我正在尝试将数据保存到数据库中。但是,当我运行时:rails server我收到以下错误:
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
I've looked at the other articles and when I run:
我看过其他文章,当我运行时:
bin/rake db:migrate
I get a rake aborted!
我得到一个 rake aborted!
After running:
运行后:
rake db:abort_if_pending_migrations....
I see that:
我看到:
You have 1 pending migration:
20150805200129 CreateDatabases
SQLite3::SQLException: table "databases" already exists:
and it just tells me to run rake db:migrate to start again.
它只是告诉我运行 rake db:migrate 重新开始。
It seems that it already exists. Is there a way to cancel the pending migration?
它似乎已经存在。有没有办法取消挂起的迁移?
回答by K M Rakibul Islam
Sometimes, even dropping a local development database is not a good idea. There are better ways to delete/destroy a specific migration in your Rails application.
有时,即使删除本地开发数据库也不是一个好主意。有更好的方法来删除/销毁 Rails 应用程序中的特定迁移。
You could use rails d migrationcommand to destroy a particular migration:
您可以使用rails d migration命令来销毁特定的迁移:
rails d migration MigrationName
To undo the changes corresponding to a particular migration, you can use db:migrate:downmethod like this:
要撤消与特定迁移相对应的更改,您可以使用如下db:migrate:down方法:
rake db:migrate:down VERSION=XXX
Sometimes, things could get more messy and in those situation another handy thing is to take a look at the schema_migrationstable in your database which has all the migrations with their versionsaved in it.
有时,事情可能会变得更加混乱,在这种情况下,另一个方便的事情是查看schema_migrations数据库中的表,该表中version保存了所有迁移。
You can delete a particular migration from this table like this:
您可以从该表中删除特定迁移,如下所示:
delete from schema_migrations WHERE version = VERSION;
if you don't want that migration to be present anymore.
如果您不想再出现这种迁移。
回答by EugZol
You migration may have failed midway (so it created the table, but didn't finish).
您的迁移可能中途失败(因此它创建了表,但没有完成)。
You are just using development environment, so it's okay to just drop database and rebuild it from scratch:
您只是在使用开发环境,因此可以删除数据库并从头开始重建它:
rake db:drop
rake db:create
rake db:migrate
回答by curt
If you are like me and maintain your database structure outside of Rails, you can just delete the migration file from db/migration. I got the error in the OP's question when I used the rails generate command to create a model class, forgetting that it also creates a migration file.
如果你像我一样在 Rails 之外维护你的数据库结构,你可以从 db/migration 中删除迁移文件。当我使用 rails generate 命令创建模型类时,我在 OP 的问题中遇到了错误,忘记了它还创建了一个迁移文件。
Do not use this method if you rely on Rails to maintain your database structure!
如果您依赖 Rails 来维护数据库结构,请不要使用此方法!
I keep my Rails structure file up to date by building it from the database using:
我通过使用以下方法从数据库构建我的 Rails 结构文件来保持它是最新的:
bundle exec rake db:structure:dump
回答by Rakshit Saxena
If you want to revert the wrong migrations, You can drop the whole db using this:
如果您想恢复错误的迁移,您可以使用以下命令删除整个数据库:
rake db:drop
Then remove the migrations file manually(This wont corrupt the db when you recreate as the Schema migrations would be dropped as well).
然后手动删除迁移文件(这不会在您重新创建时破坏数据库,因为架构迁移也会被删除)。
Then run
然后运行
rake db:migrate
And if there is data to be seeded, then run this as well
如果有数据要播种,那么也运行它
rake db:setup

