ruby-on-rails - 升级到 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
?
解决方案
有点晚了,但如果有人有同样的问题,我找到了这个解决方案:
ActiveStorage::Attachment.where(blob_id: x).destroy_all
在您的 Rails 控制台中运行它。将 x 替换为失败迁移中指定的 ID,直到没有更多无效数据,迁移通过。链接到 github 评论:https ://github.com/rails/rails/pull/33405#issuecomment-421575934
推荐阅读
- python - 如何使用python连接文件夹中的所有音频(.wav)文件?
- go - 如何从 http.Request 的响应中读取文件内容
- google-chrome-extension - 如何在 Chrome.i18n 消息字符串中添加换行符
- javascript - 如何在 htmlstring 中传递 javascript 数组的值以更改 innerHTML
- javascript - React hook:父函数如何访问子函数的状态:如果单击按钮,父应用程序会读取子函数的状态
- powershell - 为什么这个 powershell 代码片段不起作用?
- mysql - 如何在 Laravel 中对访问器进行排序?
- c++ - 如何处理以二维数组编写的条件
- powershell - 我有一个 .ps1 文件,该文件在我的项目中搜索项目证书到期,我使用 jenkins 中的批处理脚本运行该脚本
- amazon-web-services - 如何在 AWS 中释放已解除关联的弹性 IP?