32

Updated app Rails 5.2 to 6, the following two migrations were added by the update:

    # 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

and

# 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

trying to run the migrations gives the me error on the title. Haven't found anything online, any ideas on how to fix it?

wachichornia
  • 1,178
  • 14
  • 31

1 Answers1

60

The active_storage_blobs table does not exist yet. You need to first run:

rails active_storage:install

This command will add 2 migrations for 2 tables: active_storage_attachments and active_storage_blobs.

These new migrations need to be run before the migrations you have above. There is a trick for this, you can consider manually changing the timestamp in the filenames of the 2 migrations you have above to one higher than the 2 new migrations active_storage:install will create.

Once all of this is sorted, run rake db:migrate

Adim
  • 1,716
  • 12
  • 13
  • Thanks a lot. I did it once with a colleague's help and doing alone, forgot this step. My reaction was facepalm after reading your answer xD – ARK Aug 07 '20 at 12:54
  • 4
    I encountered the same error in upgrading from Rails 6.0 to 6.1. The suggested answer worked – kind of. In my case, the second (as of the OP's question) migration created in the upgrade conflicted with the newly created migration (the former was practically included in the latter). Therefore I needed to delete the *default* second migration, and the migrations worked. Hooray! BTW the commands `rails` and `rake` in the answer should be replaced with `bin/rails` in Rails 5+. – Masa Sakano Jun 26 '21 at 16:34
  • 1
    Helped me while upgrading app from rails 6.1 to 7.0, the most important part was updating the timestamp of files. – Debanjan Dey Nov 16 '21 at 15:54