ERROR: PG::NotNullViolation: ERROR: null value in column "service_name" violates not-null constraint after upgrade Rails to 7.0.6

huangapple go评论125阅读模式
英文:

ERROR: PG::NotNullViolation: ERROR: null value in column "service_name" violates not-null constraint after upgrade Rails to 7.0.6

问题

最近我们升级了 Ruby on Rails 应用(从 Ruby 2.7.2 升级到 3.2,Rails 5.2 升级到 7.0.6)。升级版本后,在本地测试时一切正常,然后我们部署了项目到 Heroku 的暂存环境。在调用延迟作业时,在保存附件时出现了以下错误:

PG::NotNullViolation: ERROR:  null value in column "service_name" of relation "active_storage_blobs" violates not-null constraint
DETAIL:  Failing row contains (7713, qNKYpuwA1jQZa4i37gRu8A4N, Stagetech1_0308, application/pdf, {"identified":true}, 210599, dmcDLApGSoTMuSfLCm5/0w==, 2023-08-03 15:58:36.169884, null).
: INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id".

尝试了激活存储的安装和 rails db:migrate,但仍然出现相同的错误。

有人可以帮助我如何修复吗?

英文:

Recently we had upgraded the Ruby on Rails app (Ruby 2.7.2 to 3.2 , Rails 5.2 to 7.0.6). After upgraded the versions, tested in local it was working as we expected, then we deployed the project into Heroku staging environment. When calling delayed jobs, while attachment saving getting below error

PG::NotNullViolation: ERROR:  null value in column \"service_name\" of relation \"active_storage_blobs\" violates not-null constraint
DETAIL:  Failing row contains (7713, qNKYpuwA1jQZa4i37gRu8A4N, Stagetech1_0308, application/pdf, {\"identified\":true}, 210599, dmcDLApGSoTMuSfLCm5/0w==, 2023-08-03 15:58:36.169884, null).
: INSERT INTO \"active_storage_blobs\" (\"key\", \"filename\", \"content_type\", \"metadata\", \"byte_size\", \"checksum\", \"created_at\") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING \"id\"".

Tried active storage installation and rails db:migrate. but getting the same error.

Can anyone help how can I fix it?

答案1

得分: 0

你的演示环境可能没有设置 config.active_storage.service 吗?

英文:

Do you maybe not have config.active_storage.service set for your staging environment?

答案2

得分: 0

Active Storage 在 active_storage_blobs 表中使用 :service_name 属性来支持多个服务。这个 service_name 属性是不可为空的。如果你没有更新数据库模式,Rails 将尝试插入空值,然后你会看到你所遇到的错误。

你可以尝试运行以下命令来填充数据库:

rails active_storage:update
rails db:migrate

如果不行,也可以尝试手动填充 service_name 为 localamazon 等值:

class BackfillServiceNameInActiveStorageBlobs < ActiveRecord::Migration[7.0]
  def up
    ActiveStorage::Blob.unscoped.where(service_name: nil).in_batches.update_all(service_name: "local")
  end

  def down
    # 无需回滚
  end
end
英文:

Active Storage uses the :service_name attribute in the active_storage_blobs table to support multiple services. This service_name attribute isn't nullable. If you haven't updated your database schema, Rails will attempt to insert null for this value and you'll get the error you're seeing.

You can try to run the following to populate db

rails active_storage:update
rails db:migrate

if not then maybe you can try to populate service_name by yourself to local, amazon, etc

class BackfillServiceNameInActiveStorageBlobs &lt; ActiveRecord::Migration[7.0]
  def up
    ActiveStorage::Blob.unscoped.where(service_name: nil).in_batches.update_all(service_name: &quot;local&quot;)
  end

  def down
    # No need to rollback
  end
end

huangapple
  • 本文由 发表于 2023年8月4日 01:10:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定