unknown attribute 'service_name' for ActiveStorage::Blob

Viewed 6923

After I upgraded from Rails 5 to 6.1, the ActiveStorage raises an error:

unknown attribute 'service_name' for ActiveStorage::Blob.

Why, and how can I fix it?

4 Answers

These commands worked for me.

rails active_storage:update
rails db:migrate

Gemfile without the error:

gem 'rails', '~> 6.0.2'

Gemfile with the error:

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

If you were already using active_storage and want to update your rails version to 6.1.0alpha, you have to run

rails active_storage:update

this will give you 2 new active_storage migrations that are needed for active_storage to work properly.

Migration 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

Migration 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

Than you just run

rails db:migrate

It works.

This is taken care of in the normal upgrade process:

rails app:update
rails db:migrate

This solution to the service_name change gets a mention in the 6.1 Release Notes. The rails app:update task calls the internal rails active_storage:update for you since Rails 6.0.0 (source).

step 1:

 rm 20191021084642_create_active_storage_tables.active_storage.rb

step 2:

 rails active_storage:install

step 3:

 rails db:migrate
Related