rake db:migrate 是将 schema.rb 与数据库架构重新同步的正确命令吗?

发布于 2024-07-19 04:18:35 字数 187 浏览 3 评论 0原文

我运行“rake db:migrate”将 schema.db 与我的数据库架构重新同步。 但它失败了,说我的一张桌子已经存在。 我认为它正在尝试重新创建表格。 如果您只想更新 schema.rb 以反映您在数据库中独立于 Rails 所做的任何更改,如果不是“rake db:migrate”,您应该使用什么命令? 关于此类事情的最佳文档来源是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

初见终念 2024-07-26 04:18:35

“rake db:migrate”将尝试为您的项目运行所有未完成的迁移。 如果您只想转储架构,请执行“rake db:schema:dump”。
但我认为你遇到了一个问题,它说该表已经存在。 您的其中一项迁移失败,因为它尝试添加的表已存在于您的数据库中。 是你亲手制作的吗? 您是否进行了迁移,但没有为此写下记录? 您需要先解决此问题,然后才能编写未来的迁移。 如果这只是一个错误,并且表格在那里并且正确,并且您想忽略它。 我的建议是通过注释掉失败的迁移中的创建表来解决这个问题。 然后运行“rake db:migrate”。 然后创建表回来。 这将更新您的架构版本。
确保对所有迁移进行正确记录。

"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.

錯遇了你 2024-07-26 04:18:35

尝试

RAILS_ENV=development rake db:drop

之前

RAILS_ENV=development rake db:migrate

并快乐!

确保在测试或开发环境中运行它,因为这会删除数据库/表

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

缱绻入梦 2024-07-26 04:18:35

我发现有时候,当事情变得有点奇怪时,您会发现自己处于这样一种情况:Rails 想要运行一个迁移,它应该正确地考虑已经完成(表已经存在,等等)。 您可以通过查找其编号(文件名开头的数字部分),进入 mysql 并发出如下查询来将迁移标记为已完成:(

insert into schema_migrations values('20090521153438');

或无论您的迁移编号是什么)

或者如果它是插件迁移使用 Desert 的 migrate_plugin 运行:

insert into plugin_schema_migrations values('my_plugin', '005');

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:

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:

insert into plugin_schema_migrations values('my_plugin', '005');
怀里藏娇 2024-07-26 04:18:35

rake db:migrate:reset 将删除所有表,运行所有迁移并创建一个新的 schema.rb 文件。

rake db:migrate:reset will drop all your tables, run all the migrations and create a new schema.rb file.

你的背包 2024-07-26 04:18:35

尝试rake db:schema:dumprake db:migrate:redo

Try rake db:schema:dump or rake db:migrate:redo.

流云如水 2024-07-26 04:18:35

使用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:dump 重新创建 db/schema.rb 文件,无需再次运行任何迁移,也无需删除任何表(这意味着丢失这些表中的数据)表),所以这是您可以首先尝试的侵入性最小的方法。

Use 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:dump re-creates the db/schema.rb file 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.

不气馁 2024-07-26 04:18:35

回答有关文档的最后一个问题:

answering your last question regarding documentation:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文