Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord::NoEnvironmentInSchemaError

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.

like image 789
Coding active Avatar asked Aug 28 '17 18:08

Coding active


2 Answers

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.

like image 122
Matt Avatar answered Nov 19 '22 02:11

Matt


This happened because, for some reason, your table ar_internal_metadata got deleted or changed. If you cannot drop the database via the command line, you need to do it via your DBMS or database client. Then, just run rails db:create db:migrate to create and run the migrations.

like image 13
Paulo Fidalgo Avatar answered Nov 19 '22 03:11

Paulo Fidalgo