diff --git a/spec/queryable_spec.cr b/spec/queryable_spec.cr index 20955a699..8381bbb92 100644 --- a/spec/queryable_spec.cr +++ b/spec/queryable_spec.cr @@ -765,14 +765,13 @@ describe Avram::Queryable do query.select_count.should eq 1 end - it "raises when used with offset or limit" do - expect_raises(Avram::UnsupportedQueryError) do - UserQuery.new.limit(1).select_count - end + it "works with distinct_on" do + UserFactory.new.age(30).create + UserFactory.new.age(30).create - expect_raises(Avram::UnsupportedQueryError) do - UserQuery.new.offset(1).select_count - end + query = UserQuery.new.distinct_on(&.age) + + query.select_count.should eq 1 end it "returns 0 if postgres returns no results" do diff --git a/src/avram/queryable.cr b/src/avram/queryable.cr index a7cca3b7e..03e0f328c 100644 --- a/src/avram/queryable.cr +++ b/src/avram/queryable.cr @@ -196,7 +196,10 @@ module Avram::Queryable(T) end def select_count : Int64 - exec_scalar(&.select_count).as(Int64) + table = "(#{query.statement}) AS temp" + new_query = Avram::QueryBuilder.new(table).select_count + result = database.scalar new_query.statement, args: query.args, queryable: schema_class.name + result.as(Int64) rescue e : DB::NoResultsError 0_i64 end