ActiveRecord::NoEnvironmentInSchemaError ActiveRecord::NoEnvironmentInSchemaError ruby ruby

ActiveRecord::NoEnvironmentInSchemaError


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/orrails 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=developmentrake db:drop RAILS_ENV=testgit checkout master# now things should workrails 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.


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.


For posterity, my issue was that this schema was generated by a rails 4 app and the current app using it is rails 5. With rails 5 the structure of the ar_internal_metadata table has changed slightly. The key field needs to be a string and contain the word 'environment', not an integer. This error only goes away when this is changed.

It should look like this in Rails 5

enter image description here

ie, change the type of ar_internatl_metadata #key to string...

My situation is a bit uncommon involving a rails 4 app and a rails 5 app sharing the same db. When I need to "update", I have a task:

  puts "Modifying Rails 4 schema to fit Rails 5 schema"  file_name = "./db/schema.rb"  rails_4_ar_internal_metadata = 'create_table "ar_internal_metadata", primary_key: "key", force: :cascade do |t|'  rails_5_ar_internal_metadata = 'create_table "ar_internal_metadata", primary_key: "key", id: :string, force: :cascade do |t|'  new_schema = File.read(file_name).gsub(rails_4_ar_internal_metadata, rails_5_ar_internal_metadata)  File.write(file_name, new_schema)