PG::UndefinedTable:错误:关系“active_storage_blobs”不存在

wac*_*nia 13 migration postgresql ruby-on-rails ruby-on-rails-6

将应用程序 Rails 5.2 更新为 6,更新添加了以下两个迁移:

    # 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
Run Code Online (Sandbox Code Playgroud)

# 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
Run Code Online (Sandbox Code Playgroud)

尝试运行迁移会在标题上出现错误。网上没找到,有什么解决办法吗?

Adi*_*dim 31

active_storage_blobs表尚不存在。您需要先运行:

rails active_storage:install
Run Code Online (Sandbox Code Playgroud)

此命令将为 2 个表添加 2 个迁移:active_storage_attachmentsactive_storage_blobs.

这些新的迁移需要在您上面的迁移之前运行。对此有一个技巧,您可以考虑手动将上述 2 个迁移的文件名中的时间戳更改为高于 2 个新迁移active_storage:install将创建的时间戳。

一旦所有这些都被排序,运行 rake db:migrate

  • 我在从 Rails 6.0 升级到 6.1 时遇到了同样的错误。建议的答案有效——有点有效。就我而言,升级中创建的第二个(根据OP的问题)迁移与新创建的迁移发生冲突(前者实际上包含在后者中)。因此,我需要删除*默认*第二次迁移,并且迁移有效。万岁!顺便说一句,答案中的命令“rails”和“rake”应该在 Rails 5+ 中替换为“bin/rails”。 (4认同)