ActiveStorage::Blob中未知属性'service_name'

40

我将Rails 5升级到6.1后,ActiveStorage会抛出一个错误:

unknown attribute 'service_name' for ActiveStorage::Blob.

为什么会这样,我该如何修复?

4个回答

73

这些命令对我来说有效。

rails active_storage:update
rails db:migrate

你在哪里找到这两个命令的?我认为最好在 Rails 上开一个问题,让他们将其添加到文档中。具体来说,是这个文档:https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-5-2-to-rails-6-0 - Rafael Gomes Francisco
1
我不确定我从哪里找到它们。我想可能是从某个文档或Rails发布中找到的。 - John Pollard
我刚刚从Rails 5.2升级到Rails 6.0,遇到了这个问题。运行命令没有帮助。但是我继续升级,当我升级到Rails 6.1时,问题就解决了。 - undefined

30
没有错误的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

它能够正常工作。


2
这应该是当前的趋势,因为每个人都将从Rails 6.0升级到Rails 6.1。 - Yshmarov

10

这在正常的升级过程中会得到处理:

rails app:update
rails db:migrate

这种解决方案在6.1版本发布说明中得到了提及。自从Rails 6.0.0起,rails app:update任务会为您调用内部的rails active_storage:update源代码)。


-1

步骤1:

 rm 20191021084642_create_active_storage_tables.active_storage.rb

步骤2:

 rails active_storage:install

步骤三:

 rails db:migrate

1
那样做不会删除你所有的附件吗? - Will

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接