gpt4 book ai didi

ruby-on-rails - PG::FeatureNotSupported:错误:聚合函数不允许更新

转载 作者:行者123 更新时间:2023-11-29 13:26:33 24 4
gpt4 key购买 nike

我收到一个 PG 错误,我无法弄清楚如何重写此语句才是可行的方法。当我在 Rails 控制台中运行它时,一切似乎都很好。但是当我尝试将其作为后台作业处理时,它会出现以下错误:

    /Users/lorenzsell/DEV/Heartbeat-pods/app/mailers/notifications_mailer.rb:44:in `community_update'
/Users/lorenzsell/DEV/Heartbeat-pods/app/jobs/send_community_digest_job.rb:17:in `block (2 levels) in perform'
/Users/lorenzsell/DEV/Heartbeat-pods/app/jobs/send_community_digest_job.rb:16:in `block in perform'
/Users/lorenzsell/DEV/Heartbeat-pods/app/jobs/send_community_digest_job.rb:8:in `perform'
/Users/lorenzsell/DEV/Heartbeat-pods/app/jobs/schedule_send_community_digest_job.rb:9:in `block in perform'
/Users/lorenzsell/DEV/Heartbeat-pods/app/jobs/schedule_send_community_digest_job.rb:8:in `each'
/Users/lorenzsell/DEV/Heartbeat-pods/app/jobs/schedule_send_community_digest_job.rb:8:in `perform'
/Users/lorenzsell/DEV/Heartbeat-pods/lib/tasks/email_tasks.rake:10:in `block (2 levels) in <top (required)>'
-e:1:in `<main>'
ActiveRecord::StatementInvalid: PG::FeatureNotSupported: ERROR: FOR UPDATE is not allowed with aggregate functions
: SELECT COUNT(*) FROM "activities" WHERE "activities"."receiver_id" = $1 AND "activities"."is_read" = 'f' FOR UPDATE
/Users/lorenzsell/DEV/Heartbeat-pods/app/mailers/notifications_mailer.rb:44:in `community_update'

这是代码片段:

community_ids = Activity.where(receiver_type: "community", is_read: false).uniq.pluck(:receiver_id)

最佳答案

根据 the documentation , lock导致 ActiveRecord 生成 SELECT ... FOR UPDATE . Postgres 不会让您对此类查询执行计数(尽管其他数据库可能——这可能是它在控制台中工作的原因),因此您需要调用 count在你的 Activity关系之前你调用lock .

# base query
notifications = Activity.where(receiver_id: options['id'], is_read: false)

# save count
notifications_count = notifications.count

# apply lock
notifications = notifications.lock(true)

关于ruby-on-rails - PG::FeatureNotSupported:错误:聚合函数不允许更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32833339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com