首页 > 解决方案 > 升级到 Rails 6 时 Active Storage 迁移出现错误

问题描述

当我将 rails 5 升级到 rails 6 并且我们在应用程序中使用活动存储时。对于主动存储,它在 Rails 6 中添加了一个新迁移。在运行此迁移时,我面临一些现有记录的以下问题

PG::ForeignKeyViolation: ERROR:  insert or update on table "active_storage_attachments" violates foreign key constraint "fk_rails_c3b3935057"
DETAIL:  Key (blob_id)=(256) is not present in table "active_storage_blobs".

有什么方法可以解决这个问题而不删除active_storage_attachments和中的记录active_storage_blobs

标签: ruby-on-railsrails-activestorageruby-on-rails-6

解决方案


有点晚了,但如果有人有同样的问题,我找到了这个解决方案:

ActiveStorage::Attachment.where(blob_id: x).destroy_all

在您的 Rails 控制台中运行它。将 x 替换为失败迁移中指定的 ID,直到没有更多无效数据,迁移通过。链接到 github 评论:https ://github.com/rails/rails/pull/33405#issuecomment-421575934


推荐阅读