本文介绍了ActiveRecord::NoEnvironmentInSchemaError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我新升级的应用程序 (Rails 5) 上执行与数据库相关的操作,但我无法在本地执行破坏性数据库命令.
rails db:resetrails db:drop .

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 .

跟踪结果如下,

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. 设置 bin/rails db:environment:set RAILS_ENV=development,不会改变任何内容,仍然会出现错误.
  2. 手动设置环境变量以进行开发.
  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.

推荐答案

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.

此不正确的错误消息是 此拉取请求的结果,旨在防止破坏性对生产数据库的操作.正如 u/pixelearth 正确指出的,Rails 4.2 在 ar_internal_metadata 中定义了key"字段table 是一个整数,Rails 5+(包括 Rails 6)期望它是一个带有 environment 值的字符串.当 Rails 5 和 Rails 6 运行此安全检查时,它们错误地引发了 ActiveRecord::NoEnvironmentInSchemaError,因为代码与 Rails 4 模式格式不兼容.

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.

**请记住,安全检查是由于 如此多的用户意外删除了他们的生产数据库.正如问题所描述的,这些操作是破坏性的.

**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.

来自终端:

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

如前所述此处DISABLE_DATABASE_ENVIRONMENT_CHECK=1 标志禁用环境检查.之后,您可以运行 rake db:create RAILS_ENV=development,例如,使用 ar_internals_metadata 表中的正确架构重新创建数据库.

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.

来自终端:

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.

这篇关于ActiveRecord::NoEnvironmentInSchemaError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 15:14