【问题标题】:ROR heroku PostGres issueROR heroku PostGres 问题
【发布时间】:2010-04-24 04:04:54
【问题描述】:

出现错误:

ActiveRecord::StatementInvalid (PGError: ERROR:  argument of HAVING must be type boolean, not type timestamp without time zone

控制器代码sn-p:

 def inactive
    @number_days = params[:days].to_i || 90
    @clients = Client.find(:all,
      :include => :appointments,
      :conditions => ["clients.user_id = ? AND appointments.start_time <= ?", current_user.id, @number_days.days.ago],
      :group => 'client_id',
      :having => 'MAX(appointments.start_time)'
    )
  end

已更改
:having =&gt; 'MAX(appointments.start_time)'

:having =&gt; ['MAX(appointments.start_time) &lt;= ?', @number_days.days.ago]
现在错误是:
ActiveRecord::StatementInvalid (PGError: ERROR: column "clients.id" must appear in the GROUP BY clause or be used in an aggregate function

【问题讨论】:

  • 我建议您复习一下 SQL 的 WHERE、HAVING 和 GROUP BY 子句。 w3schools.com/sql/sql_having.asp。这不是 Rails 问题。
  • 是的,它绝对是postgres 比sqlite 更严格,只是想找到一个解决方案....还在学习

标签: ruby-on-rails ruby postgresql heroku


【解决方案1】:

:having 子句需要一个计算结果为布尔值的 SQL sn-p。 MAX(appointments.start_time) 计算为时间戳

【讨论】:

    【解决方案2】:

    将 :group => 'client_id' 更改为 :group => 'table_name.client_id'

    【讨论】:

      猜你喜欢
      • 2019-08-15
      • 2012-09-14
      • 1970-01-01
      • 2012-06-08
      • 2017-09-30
      • 1970-01-01
      • 2019-08-05
      • 2011-08-17
      • 1970-01-01
      相关资源
      最近更新 更多