Rails 5, Rspec: Environment data not found in the schema
Solution 1
New Rails 5 command to generate binstubs:
rails app:update:bin
Allows me to run the solution as the error suggested:
bin/rails db:environment:set RAILS_ENV=test
Tip from @max comment: If you are using database_cleaner
and this error keeps popping up then change your config to:
DatabaseCleaner.clean_with(
:truncation,
except: %w(ar_internal_metadata)
)
Solution 2
For me, this error was followed by a similar one asking for a migration.
This did the trick: rails db:migrate RAILS_ENV=test
Solution 3
fix for jenkins before you drop database you should execute db:environment:set
with || true
, so the command doesn't abort:
bin/rails db:environment:set RAILS_ENV=test || true
Solution 4
All of the above answers are correct, however, if you're in a more unique project such as developing a rails engine with a concept of a schema (hacky, I know) and your migration fails for some reason, you can re-run it without the check that throws this exception. Example:
rake environment db:{drop,create,migrate} DISABLE_DATABASE_ENVIRONMENT_CHECK=1
Solution 5
For me, I had to do a mixture of things:
bin/rails db:environment:set RAILS_ENV=test
bin/rails db:migrate RAILS_ENV=test
This would make things work, and then I had to review my migrations, I was adding a null:false into a relationship and that added a bug, the migration was cancelled and didn't finish
Comments
-
steel about 4 years
After upgrading a Rails app to Rails 5, running RSpec tests gives me the following error:
rails aborted! ActiveRecord::NoEnvironmentInSchemaError: Environment data not found in the schema. To resolve this issue, run: bin/rails db:environment:set RAILS_ENV=test
However, that
bin
does not exist and I can't seem to generate it withbundle binstubs rails
or withrake rails:update:bin
.I have also tried:
rails db:environment:set RAILS_ENV=test rake db:environment:set RAILS_ENV=test
There is a related issue on Github here.
How can I address this error?
-
max over 7 yearsIf you are using
database_cleaner
and this error keeps popping up then change your config toDatabaseCleaner.clean_with :truncation, except: %w(ar_internal_metadata)
- github.com/DatabaseCleaner/database_cleaner/issues/445 -
Frexuz about 6 yearsomg, the db_cleaner solution works! :P didn't know the env was set in that table.
-
rigyt almost 6 yearsWhat file does this code go for minitest? Error still ocurring with code in test/test_helper.rb
-
x-yuri almost 6 years
-
James Hibbard almost 5 years4.
bundle exec rake db:migrate RAILS_ENV=test
-
Grant Birchmeier over 4 yearsThat
%w(ar_internal_metadata)
workaround did not work for me. (Rails 6, cucumber-rails 2.0.0, mysql). Any other ideas? -
Grant Birchmeier over 4 years^^ and database_cleaner 1.7.0 (so it's def not fixed in 1.6.2)
-
Nathan L. Walls almost 4 yearsIt appears this was fixed as of 1.8.2. Running a test suite with repeat has yet to bring the message back up after: 1. Updating the gem 2. Resetting my environment data