Ruby – ActiveRecord::NoEnvironmentInSchemaError

rubyruby-on-rails-5

I'm trying to perform database related operations on my newly upgraded app(Rails 5) and I'm unable to perform destructive database commands locally.
rails db:reset or rails db:drop .

The trace results with the following data,

rails db:drop --trace
** Invoke db:drop (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Invoke db:check_protected_environments (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config
** Execute db:check_protected_environments

rails aborted!
ActiveRecord::NoEnvironmentInSchemaError: 

Environment data not found in the schema. To resolve this issue, run: 

    bin/rails db:environment:set RAILS_ENV=development

What I've tried so far are,

  1. Setting bin/rails db:environment:set RAILS_ENV=development, doesn't change anything still the error occurs.
  2. Setting Environment variable manually to development.

None of these helped. I'm Looking for a fix or workaround.

Best Answer

Two Fixes to ActiveRecord::NoEnvironmentInSchemaError

The other answers here describe the problem very well, but lack proper solutions. Hoping this answer helps someone experiencing this issue.

Why this error is happening

This incorrect error message is a result of this pull request designed to prevent destructive actions on production databases. As u/pixelearth correctly points out, Rails 4.2 defines the 'key' field in the ar_internal_metadata table to be an integer, and Rails 5+ (including Rails 6) expects this to be a string with the value, environment. When Rails 5 and Rails 6 run this safety check, they incorrectly raise an ActiveRecord::NoEnvironmentInSchemaError as the code is incompatible with the Rails 4 schema format.

Fix #1: Override the safety check in Rails 5+

**Please remember, the safety check was implemented as a result of so many users dropping their production databases by accident. As the question describes, the operations are destructive.

From the terminal:

rails db:drop RAILS_ENV=development DISABLE_DATABASE_ENVIRONMENT_CHECK=1
# and/or
rails db:drop RAILS_ENV=test DISABLE_DATABASE_ENVIRONMENT_CHECK=1

As noted here, the DISABLE_DATABASE_ENVIRONMENT_CHECK=1 flag disables the environment check. After, you can run a rake db:create RAILS_ENV=development, for example, to recreate your database with the correct schema in the ar_internals_metadata table.

Fix #2: Revert to Rails 4, drop database, go back to Rails 5+ and recreate

From the terminal:

git log
# grab the commit hash from before the upgrade to Rails 5+

git checkout **hash_from_rails_4**
rake db:drop RAILS_ENV=development
rake db:drop RAILS_ENV=test

git checkout master

# now things should work
rails db:migrate

Again, please ensure you are not pointing at a production database when overriding this functionality. Alternatively, it would be possible to directly modify the schema of this table. If you're experiencing this error in production, you may need to take this approach.

Related Topic