【问题标题】:unknown attribute 'service_name' for ActiveStorage::BlobActiveStorage::Blob 的未知属性“service_name”
【发布时间】:2020-02-10 20:45:19
【问题描述】:

我从 Rails 5 升级到 6.1 后,ActiveStorage 报错:

unknown attribute 'service_name' for ActiveStorage::Blob.

为什么,我该如何解决?

【问题讨论】:

    标签: ruby-on-rails-6


    【解决方案1】:

    这些命令对我有用。

    rails active_storage:update
    rails db:migrate
    

    【讨论】:

    • 你在哪里找到这两个命令?我认为在 Rails 上打开一个问题以将其添加到 doc 上是件好事。具体来说,这个文档:guides.rubyonrails.org/…
    • 我不确定在哪里找到它们。我想它来自一些文档或 Rails 版本。
    【解决方案2】:

    没有错误的Gemfile:

    gem 'rails', '~> 6.0.2'
    

    出现错误的 Gemfile:

    gem 'rails', github: 'rails/rails', branch: 'master'
    

    如果您已经在使用 active_storage 并且想要将您的 rails 版本更新到 6.1.0alpha,则必须运行

    rails active_storage:update
    

    这将为您提供 2 个新的 active_storage 迁移,这些迁移是 active_storage 正常工作所必需的。

    迁移 1:

    # This migration comes from active_storage (originally 20190112182829)
    class AddServiceNameToActiveStorageBlobs < ActiveRecord::Migration[6.0]
      def up
        unless column_exists?(:active_storage_blobs, :service_name)
          add_column :active_storage_blobs, :service_name, :string
    
          if configured_service = ActiveStorage::Blob.service.name
            ActiveStorage::Blob.unscoped.update_all(service_name: configured_service)
          end
    
          change_column :active_storage_blobs, :service_name, :string, null: false
        end
      end
    end
    

    迁移 2:

    # This migration comes from active_storage (originally 20191206030411)
    class CreateActiveStorageVariantRecords < ActiveRecord::Migration[6.0]
      def up
        create_table :active_storage_variant_records do |t|
          t.belongs_to :blob, null: false, index: false
          t.string :variation_digest, null: false
    
          t.index %i[ blob_id variation_digest ], name: "index_active_storage_variant_records_uniqueness", unique: true
          t.foreign_key :active_storage_blobs, column: :blob_id
        end
      end
    end
    

    比你刚刚运行

    rails db:migrate
    

    有效。

    【讨论】:

    • 这应该是现在的趋势,因为每个人都将从 rails 6.0 升级到 rails 6.1
    【解决方案3】:

    这是在正常升级过程中处理的:

    rails app:update
    rails db:migrate
    

    service_name 更改的此解决方案在6.1 Release Notes 中得到提及。从 Rails 6.0.0 (source) 开始,rails app:update 任务会为您调用内部的rails active_storage:update

    【讨论】:

      【解决方案4】:

      第一步:

       rm 20191021084642_create_active_storage_tables.active_storage.rb
      

      第二步:

       rails active_storage:install
      

      第三步:

       rails db:migrate
      

      【讨论】:

      • 不会删除所有附件吗?
      猜你喜欢
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 2023-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-07
      相关资源
      最近更新 更多