为什么要求我运行“rake db:migrate RAILS

编程入门 行业动态 更新时间:2024-10-24 12:31:10
本文介绍了为什么要求我运行“rake db:migrate RAILS_ENV=test"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在 Rails 4.0.0.rc1、Ruby 2.0.0 上运行迁移后,当我尝试通过 rspec 运行测试时看到以下错误:

On Rails 4.0.0.rc1, Ruby 2.0.0, after I run a migration, I see the following error when I try to run a test through rspec:

/Users/peeja/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.rc1/lib/active_record/migration.rb:376:in`check_pending!':迁移正在等待;运行 'rake db:migrateRAILS_ENV=test' 来解决这个问题.(ActiveRecord::PendingMigrationError)

/Users/peeja/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.rc1/lib/active_record/migration.rb:376:in `check_pending!': Migrations are pending; run 'rake db:migrate RAILS_ENV=test' to resolve this issue. (ActiveRecord::PendingMigrationError)

这似乎不对.没有人迁移他们的测试数据库,是吗?他们db:test:prepare 它,公平地说,我忘了这样做.所以我运行 rake db:test:prepare 并再次运行我的 rspec 命令......并看到相同的错误.

That doesn't seem right. No one migrates their test database, do they? They db:test:prepare it, which—to be fair—I've forgotten to do. So I run rake db:test:prepare and run my rspec command again…and see the same error.

如果我真的rake db:migrate RAILS_ENV=test,错误确实会消失.

If I actually rake db:migrate RAILS_ENV=test, the error does in fact go away.

这是怎么回事?这是 Rails 4 的新功能吗?

What's going on? Is this new in Rails 4?

推荐答案

从 Rails 4.1 开始,不推荐使用 rake db:test:* 任务.相反,您的 (test|spec)_helper.rb 应该包括:

As of Rails 4.1, the rake db:test:* tasks are deprecated. Instead, your (test|spec)_helper.rb should include:

ActiveRecord::Migration.maintain_test_schema!

这意味着每次运行测试时,您的测试数据库都会获得正确的架构,无论您是否从 Rake 任务中运行它们.

This means that your test database will get the correct schema every time your tests run, whether you run them from a Rake task or not.

更多推荐

为什么要求我运行“rake db:migrate RAILS

本文发布于:2023-10-28 13:28:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536768.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:db   rake   RAILS   migrate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!