Skip to content

Commit 1812568

Browse files
committed
Include selects in group query with having clause
When a grouped calculation contains a having clause that references a selected value, we need to include that selected value in the query. Postgres doesn't support referencing a selected value in a having clause, but other databases do; we can skip the test on the pg adapter but run it for the others. This was fixed before in 9a298a1, but the test coverage was lost in 5a05207. The fix regressed in 6311975 and was removed in 97d46c1.
1 parent 38d05ce commit 1812568

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

activerecord/lib/active_record/relation/calculations.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
282282
operation,
283283
distinct).as(aggregate_alias)
284284
]
285+
select_values += self.select_values unless having_clause.empty?
285286

286287
select_values.concat group_columns.map { |aliaz, field|
287288
if field.respond_to?(:as)

activerecord/test/cases/calculations_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ def test_should_group_by_summed_field_having_condition
246246
end
247247

248248
def test_should_group_by_summed_field_having_condition_from_select
249-
c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("MIN(credit_limit) > 50").sum(:credit_limit)
249+
skip if current_adapter?(:PostgreSQLAdapter)
250+
c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("min_credit_limit > 50").sum(:credit_limit)
250251
assert_nil c[1]
251252
assert_equal 60, c[2]
252253
assert_equal 53, c[9]

0 commit comments

Comments
 (0)