【问题标题】:Rails 4, Ruby 2.7.1 schema.rb shows "Could not dump table because of following FrozenError"Rails 4, Ruby 2.7.1 schema.rb 显示“Could not dump table because of following FrozenError”
【发布时间】:2020-06-25 08:03:42
【问题描述】:

升级到 Ruby 2.7.1 后,我的 schema.rb 出现了多个表的以下警告:

# Could not dump table "pages" because of following FrozenError
#   can't modify frozen String: "false"

# Could not dump table "proxies" because of following FrozenError
#   can't modify frozen String: "true"

我已经到处搜索了解决方案,并检查了(尽我所能)迁移,其中有很多。 在这个阶段降级 Ruby 不是一种选择。

有人遇到过类似的情况并设法解决了吗?

【问题讨论】:

  • 您使用的是哪个特定版本的 Rails?
  • 我正在运行 Rails 4.2.11
  • 试图复制您的问题,我什至无法运行迁移。有没有办法重现您的问题?
  • 您需要将 ruby​​ 降级到 2.4.5 左右或升级 rails。这是一个针对 rails 5.x 的已解决问题,称由于许多警告和问题,不支持 2.7:github.com/rails/rails/issues/38426
  • 感谢@mlockerd 我想是时候升级 Rails 了:|

标签: ruby ruby-on-rails-4 schema ruby-2.7


【解决方案1】:

如果有人还在寻找这个。

该错误是因为在旧版本的 Rails 中,模式转储程序使用 to_s 将数据库默认列值转换为字符串,然后对该字符串执行操作。但是在ruby 2.7 中,nil, true and falseto_s 方法返回了一个冻结的字符串,因此返回了FrozenError,所以如果升级rails 版本不是一个选项,你可以把它放在一个初始化器中来覆盖模式转储器方法来使用dup 而不是实际的字符串。

module ActiveRecord
  module ConnectionAdapters
    module ColumnDumper
      def prepare_column_options(column, types)
        spec = {}
        spec[:name]      = column.name.inspect
        spec[:type]      = column.type.to_s
        spec[:null]      = 'false' unless column.null

        limit = column.limit || types[column.type][:limit]
        spec[:limit]     = limit.inspect if limit
        spec[:precision] = column.precision.inspect if column.precision
        spec[:scale]     = column.scale.inspect if column.scale

        default = schema_default(column).dup if column.has_default?
        spec[:default]   = default unless default.nil?

        spec
      end
    end
  end
end

【讨论】:

  • 这有帮助,谢谢!
【解决方案2】:

我知道你提到降级 RUby 不是你的选择。

但是,如果您能够降级 Ruby,这对我有用。

rmlockerd 指出这是一个 Ruby 2.7 问题。

我最终将我的 Ruby 版本降级为“2.6.6”。

在降级我的 Ruby 后,我运行 db:schema:load 并能够重新生成架构文件而不会出现上述错误。

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 2022-12-02
    相关资源
    最近更新 更多