ActiveRecord::StatementInvalid: 找不到表 'users' Rails 5 教程 [英] ActiveRecord::StatementInvalid: Could not find table 'users' Rails 5 tutorial
问题描述
在内置的示例应用程序 (Michael Hartl Rails 5) 上运行 Rails 测试,运行测试时出现上述错误,这表明它找不到表 'users',它在我的 db migrate 文件夹中可用,也列在 development.sqlite3 文件中,所以不确定是什么问题
Running tests on Rails on the built Sample App (Michael Hartl Rails 5), When running tests I get the above error, which suggests it can't find the table 'users', it's available in my db migrate folder and also listed in the development.sqlite3 file, So not sure what the issue is
尝试了推荐的修复运行 rake db:test:prepare, rails db:migrate:reset testing 以查看 User.new(name: 'foo') 创建的用户是否没有解决问题,后者在控制台所以无法理解为什么它找不到表格
Tried the recommended fixes running rake db:test:prepare, rails db:migrate:reset testing to see if User.new(name: 'foo') creates a user neither have fixed the problem and the latter creates fine in the console so can't understand why it can't find the table
_create_users.rb
_create_users.rb
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps
end
end
end
尝试运行rails test:mailers"时运行错误的测试以及使用rails test"时的更多测试
tests to run errors run when attempting 'rails test:mailers' and more when using 'rails test'
推荐答案
请务必记住,Rails 中的每个环境都有自己的数据库.仅仅因为您的应用程序在您进行开发时正在运行 (RAILS_ENV=development
),并不意味着在运行测试时数据库就在那里 (RAILS_ENV=test
).
It's important to remember that each environment in Rails has its own database. Just because your application is working when you're doing development (RAILS_ENV=development
), it doesn't mean the database is there when running tests (RAILS_ENV=test
).
您可能只需要准备您的测试数据库(创建并迁移它):
You may simply need to prepare your test database (create it and migrate it):
# Drop any test DB you already have
bundle exec rake db:drop RAILS_ENV=test
# Create a clean fresh one
bundle exec rake db:create RAILS_ENV=test
# Import the Schema from your schema.rb, rather than run all migrations
bundle exec rake db:schema:load RAILS_ENV=test
如果您执行上述操作,然后运行测试,它们应该会按预期工作.
If you do the above and then run your tests, they should work as expected.
这篇关于ActiveRecord::StatementInvalid: 找不到表 'users' Rails 5 教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!