两个修复ActiveRecord::NoEnvironmentInSchemaError
这里的其他答案很好地描述了问题,但缺乏适当的解决方案。希望这个答案可以帮助遇到此问题的人。
为什么会发生这个错误
此错误消息的原因是这个拉取请求 https://github.com/rails/rails/pull/22967旨在防止对生产数据库的破坏性操作。作为u/pixelearth 正确指出 https://stackoverflow.com/a/56309734/1038034, Rails 4.2 定义了 'key' 字段ar_internal_metadata
table 是一个整数,Rails 5+(包括 Rails 6)期望它是一个带有值的字符串,environment
。当 Rails 5 和 Rails 6 运行此安全检查时,它们会错误地提出ActiveRecord::NoEnvironmentInSchemaError
因为代码与 Rails 4 架构格式不兼容。
修复 #1:覆盖 Rails 5+ 中的安全检查
**请记住,安全检查的实施是由于很多用户不小心丢失了他们的生产数据库 https://blog.bigbinary.com/2016/06/07/rails-5-prevents-destructive-action-on-production-db.html。正如问题所描述的,这些操作具有破坏性。
从航站楼:
rails db:drop RAILS_ENV=development DISABLE_DATABASE_ENVIRONMENT_CHECK=1
# and/or
rails db:drop RAILS_ENV=test DISABLE_DATABASE_ENVIRONMENT_CHECK=1
如前所述here https://blog.bigbinary.com/2016/06/07/rails-5-prevents-destructive-action-on-production-db.html, the DISABLE_DATABASE_ENVIRONMENT_CHECK=1
标志禁用环境检查。之后,您可以运行rake db:create RAILS_ENV=development
,例如,使用正确的模式重新创建数据库ar_internals_metadata
table.
修复 #2:恢复到 Rails 4,删除数据库,返回到 Rails 5+ 并重新创建
从航站楼:
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
再次,请确保您在覆盖此功能时没有指向生产数据库。或者,可以直接修改该表的模式。如果您在生产中遇到此错误,您可能需要采取此方法。