PG::UndefinedTable: ERROR: 关系表 "active_storage_blobs" 不存在。

32

将应用程序从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

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

尝试运行迁移时出现标题错误。在网上没有找到任何信息,有什么修复方法吗?
1个回答

61

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

rails active_storage:install

这条命令将为两个表添加2个迁移:active_storage_attachmentsactive_storage_blobs

这些新的迁移需要在您上面的迁移之前运行。有一个技巧,您可以手动更改上面2个迁移的文件名中的时间戳,使其比 active_storage:install 创建的2个新迁移的时间戳高一位。

一旦所有问题都解决了,请运行 rake db:migrate 命令。


非常感谢。我曾经在同事的帮助下完成过,但是自己做的时候忘记了这一步骤。看到你的回答后,我的反应是面掌笑 xD。 - ARK
4
我在将 Rails 6.0 升级到 6.1 时遇到了相同的错误。建议的解决方案有点管用。在我的情况下,升级中创建的第二个(与 OP 的问题相同)迁移文件与新创建的迁移文件有冲突(前者实际上包含在后者中)。因此,我需要删除默认的第二个迁移文件,然后这些迁移就运行成功了。太好了!顺便说一句,答案中的命令 railsrake 应该在 Rails 5+ 中替换为 bin/rails - Masa Sakano
1
在将应用程序从Rails 6.1升级到7.0时,帮助了我,最重要的部分是更新文件的时间戳。 - Debanjan Dey

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