从 Rails 5 升级到 6.1 后,ActiveStorage 引发错误:
unknown attribute 'service_name' for ActiveStorage::Blob.
为什么,我该如何解决?
Joh*_*ard 40
这些命令对我有用。
rails active_storage:update
rails db:migrate
Run Code Online (Sandbox Code Playgroud)
Ysh*_*rov 22
没有错误的 Gemfile:
gem 'rails', '~> 6.0.2'
Run Code Online (Sandbox Code Playgroud)
带有错误的 Gemfile:
gem 'rails', github: 'rails/rails', branch: 'master'
Run Code Online (Sandbox Code Playgroud)
如果您已经在使用 active_storage 并且想要将 Rails 版本更新到 6.1.0alpha,则必须运行
rails active_storage:update
Run Code Online (Sandbox Code Playgroud)
这将为您提供 2 个新的 active_storage 迁移,这些迁移是 active_storage 正常工作所需的。
迁移 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
Run Code Online (Sandbox Code Playgroud)
迁移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
Run Code Online (Sandbox Code Playgroud)
比你跑
rails db:migrate
Run Code Online (Sandbox Code Playgroud)
有用。
这是在正常升级过程中处理的:
rails app:update
rails db:migrate
Run Code Online (Sandbox Code Playgroud)
6.1 发行说明中service_name提到了此更改的解决方案。从 Rails 6.0.0 ( source )开始,该任务会为您调用内部函数。rails app:updaterails active_storage:update
小智 -1
步骤1:
rm 20191021084642_create_active_storage_tables.active_storage.rb
Run Code Online (Sandbox Code Playgroud)
第2步:
rails active_storage:install
Run Code Online (Sandbox Code Playgroud)
步骤3:
rails db:migrate
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4019 次 |
| 最近记录: |