Ruby-on-rails rake db:migrate 是将 schema.rb 与您的数据库架构重新同步的正确命令吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/842162/
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
Is rake db:migrate the correct command to re-sync schema.rb with your database schema?
提问by drizzle
I ran "rake db:migrate" to re-sync schema.db with my database schema. But it failed, saying that one of my tables already exists. I think it was trying to re-create the table. If you just want to get schema.rb updated to reflected any changes you made in the database independently of Rails, what command should you use if not "rake db:migrate"? And what's the best source of documentation on this type of thing?
我运行“rake db:migrate”以将schema.db 与我的数据库架构重新同步。但它失败了,说我的一张表已经存在。我认为它试图重新创建表格。如果您只想更新 schema.rb 以反映您在独立于 Rails 的数据库中所做的任何更改,如果不是“rake db:migrate”,您应该使用什么命令?关于此类事情的最佳文档来源是什么?
回答by scottd
"rake db:migrate" will try and run all the outstanding migrations for your project. If you just want to dump the schema do a "rake db:schema:dump".
But I think you have a problem where it says that the table already exists. One of your migrations is failing because the table it is trying to add already exists in your db. Did you create one by hand? Did you down a migration, but not have a down written for it? You will need to fix this before you can write future migrations. If it is just a mistake, and the table is there and correct and you want to ignore this. My recommendation is to hack around it by commenting out the create table in the failing migration. Then run "rake db:migrate". Then but the create table back. This will update your schema version.
Make sure you write proper downs on all migrations.
"rake db:migrate" 将尝试为您的项目运行所有未完成的迁移。如果您只想转储模式,请执行“rake db:schema:dump”。
但我认为你有一个问题,它说表格已经存在。您的一次迁移失败,因为它尝试添加的表已存在于您的数据库中。你是手工制作的吗?您是否进行了迁移,但没有为它写下来?您需要先修复此问题,然后才能编写未来的迁移。如果这只是一个错误,并且表格在那里并且是正确的,并且您想忽略它。我的建议是通过在失败的迁移中注释掉创建表来绕过它。然后运行“rake db:migrate”。然后,但创建表回来了。这将更新您的架构版本。
确保你写下所有迁移的正确记录。
回答by crmoreira
Try
尝试
RAILS_ENV=development rake db:drop
before
前
RAILS_ENV=development rake db:migrate
and be happy!
而且要快乐!
Making sure that you run it on your test or development environment since this will drop the database/tables
确保在测试或开发环境中运行它,因为这会删除数据库/表
回答by hoff2
I've found that occasionally, when things get a little weird, you'll find yourself in a situation where Rails will want to run a migration that it ought rightly to be considering already done (the table already exists, etc). You can mark a migration as done by finding its number (the number part at the beginning of the filename), going into mysql and issuing a query like so:
我发现偶尔,当事情变得有点奇怪时,您会发现自己处于这样一种情况:Rails 想要运行它应该正确考虑已经完成的迁移(表已经存在等)。您可以通过查找其编号(文件名开头的数字部分),进入 mysql 并发出如下查询来将迁移标记为已完成:
insert into schema_migrations values('20090521153438');
(or whatever the number of your migration is)
(或无论您的迁移次数是多少)
Or if it's a plugin migration being run using Desert's migrate_plugin:
或者,如果它是使用 Desert 的 migrate_plugin 运行的插件迁移:
insert into plugin_schema_migrations values('my_plugin', '005');
回答by Mike
rake db:migrate:resetwill drop all your tables, run all the migrations and create a new schema.rbfile.
rake db:migrate:reset将删除所有表,运行所有迁移并创建一个新schema.rb文件。
回答by Thomas Klemm
Use rake db:schema:dump.
使用rake db:schema:dump.
$ rake -T | grep schema
rake db:schema:dump # Create a db/schema.rb file that is portable
# against any database supported by ActiveRecord
rake db:schema:dumpre-creates the db/schema.rbfile without running any of your migrations again, or dropping any tables (which implicates losing the data in those tables), so it's the least invasive way you can try first.
rake db:schema:dump重新创建db/schema.rb文件而不再次运行任何迁移或删除任何表(这意味着丢失这些表中的数据),因此这是您可以首先尝试的侵入性最小的方法。
回答by pts
Try rake db:schema:dumpor rake db:migrate:redo.
尝试rake db:schema:dump或rake db:migrate:redo。
回答by Maximiliano Guzman
answering your last question regarding documentation:
回答您关于文档的最后一个问题:
- a guide to rails rake tasks for rails 2.1+: http://www.tutorialspoint.com/ruby-on-rails-2.1/rails-and-rake.htm
- a good screencast on migrations: http://railscasts.com/episodes/107-migrations-in-rails-2-1
- rails 2.1+ 的 rails rake 任务指南:http://www.tutorialspoint.com/ruby-on-rails-2.1/rails-and-rake.htm
- 关于迁移的一个很好的截屏视频:http: //railscasts.com/episodes/107-migrations-in-rails-2-1

