38

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?

greybeard
  • 2,249
  • 8
  • 30
  • 66
Pringles
  • 421
  • 6
  • 9

4 Answers4

71

These commands worked for me.

rails active_storage:update
rails db:migrate
John Pollard
  • 3,729
  • 3
  • 24
  • 50
  • Where you found these two commands? I think so is good to open an issue on rails to they add it on doc. Specifically, this doc: https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-5-2-to-rails-6-0 – Rafael Gomes Francisco Jan 27 '21 at 04:07
  • 1
    I'm not sure where I found them. I imagine it was from some document or rails release. – John Pollard Feb 02 '21 at 14:45
30

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.

Yshmarov
  • 3,450
  • 1
  • 22
  • 41
10

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).

Anson
  • 6,575
  • 2
  • 39
  • 33
-1

step 1:

 rm 20191021084642_create_active_storage_tables.active_storage.rb

step 2:

 rails active_storage:install

step 3:

 rails db:migrate
Milad Bahmanabadi
  • 946
  • 11
  • 27