Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

ActiveRecord count doesn't work with joins().uniq properly #6865

Closed
dpisarewski opened this Issue Jun 26, 2012 · 12 comments

Comments

Projects
None yet
jruby-1.6.7 :060 > Employee.joins(:projects).uniq.all.size
  Employee Load (17.0ms)  
SELECT DISTINCT `employees`.* 
FROM `employees` 
INNER JOIN `assignments` ON `assignments`.`employee_id` = `employees`.`id` 
INNER JOIN `projects` ON `projects`.`id` = `assignments`.`project_id`
 => 87 

jruby-1.6.7 :061 > Employee.joins(:projects).uniq.count
   (4.0ms)  
SELECT DISTINCT COUNT(*) 
FROM `employees` 
INNER JOIN `assignments` ON `assignments`.`employee_id` = `employees`.`id` 
INNER JOIN `projects` ON `projects`.`id` = `assignments`.`project_id`
 => 910 
Owner

rafaelfranca commented Jun 26, 2012

@dpisarewski have you tried in master?

I could reproduce it on master.

User.joins(:activities).all.uniq.size
SELECT "users".* FROM "users" INNER JOIN "activities" ON "activities"."user_id" = "users"."id"
 => 1

User.joins(:activities).uniq.count
SELECT DISTINCT COUNT(*) FROM "users" INNER JOIN "activities" ON "activities"."user_id" = "users"."id"
 => 2 

It seems to me like User.joins(:activities).uniq.count should generate

SELECT COUNT(DISTINCT(users.*)) FROM "users" INNER JOIN "activities" ON "activities"."user_id" = "users"."id"

fcy commented Aug 19, 2012

+1 I'm having this problem and the proposed solution (distinct inside count) would fix it

The only workaround I could find is to call .to_a and the .size

jsuwo commented Aug 27, 2012

+1 Also encountering this issue (see my scenario in issue #4016, if you need another example. Just add .uniq to the end of the .for scope).

Contributor

tbaba commented Oct 5, 2012

👍 I also got this.

Contributor

sishen commented Oct 15, 2012

I think the problem is that the generated SQL is wrong, even without joins.

> User.count
   (0.4ms)  SELECT COUNT(*) FROM `users`
> User.uniq
  User Load (0.2ms)  SELECT DISTINCT `users`.* FROM `users`
> User.uniq.count
   (0.3ms)  SELECT DISTINCT COUNT(*) FROM `users`

I expected the sql should be "**SELECT COUNT(DISTINCT users.*) FROM users**

Contributor

maxim commented Oct 16, 2012

This keeps biting me too. Would be very grateful to whoever could take time to fix it.

Contributor

paulkoegel commented Nov 13, 2012

having the same problem. would be awesome if it got fixed.

hueezer commented Dec 16, 2012

I'm also experiencing the same issue!

Contributor

seejee commented Dec 16, 2012

I'm looking into this, and I think it's actually a problem in arel. I'll continue to dig and will file an issue and/or PR against arel once I've narrowed it down.

I am having the same problem. Thanks if someone can fix it.

Just to +1 this.

> scope.size
=> 27
> scope.length
=> 23

@senny senny closed this in 648def4 Jan 26, 2013

@waldenraines waldenraines added a commit to waldenraines/katello that referenced this issue Jun 17, 2015

@waldenraines waldenraines Fixes BZ{1198349,1222365,1222673,1228278,1232510}: fix errata count.
There were several places in the UI where the errata count was
off due to downstream using an old version of activerecord with
a bug in it.  Upstream we are using 3.2.21 but downstream we are
stuck with 3.2.8.  The activerecord bug was fixed in 3.2.13.

https://bugzilla.redhat.com/show_bug.cgi?id=1198349
https://bugzilla.redhat.com/show_bug.cgi?id=1222365
https://bugzilla.redhat.com/show_bug.cgi?id=1222673
https://bugzilla.redhat.com/show_bug.cgi?id=1228278
https://bugzilla.redhat.com/show_bug.cgi?id=1232510

See also rails/rails#6865
617733b

@waldenraines waldenraines added a commit to waldenraines/katello that referenced this issue Jun 18, 2015

@waldenraines waldenraines Fixes BZ{1198349,1222365,1222673,1228278,1232510}: fix errata count.
There were several places in the UI where the errata count was
off due to downstream using an old version of activerecord with
a bug in it.  Upstream we are using 3.2.21 but downstream we are
stuck with 3.2.8.  The activerecord bug was fixed in 3.2.13.  This
commit monkey patches 3.2.8 with the fix.

https://bugzilla.redhat.com/show_bug.cgi?id=1198349
https://bugzilla.redhat.com/show_bug.cgi?id=1222365
https://bugzilla.redhat.com/show_bug.cgi?id=1222673
https://bugzilla.redhat.com/show_bug.cgi?id=1228278
https://bugzilla.redhat.com/show_bug.cgi?id=1232510

See also rails/rails#6865
6c4e8c1

@waldenraines waldenraines added a commit to waldenraines/katello that referenced this issue Jun 19, 2015

@waldenraines waldenraines Fixes BZ{1198349,1222365,1222673,1228278,1232510}: fix errata count.
There were several places in the UI where the errata count was
off due to downstream using an old version of activerecord with
a bug in it.  Upstream we are using 3.2.21 but downstream we are
stuck with 3.2.8.  The activerecord bug was fixed in 3.2.13.  This
commit monkey patches 3.2.8 with the fix.

https://bugzilla.redhat.com/show_bug.cgi?id=1198349
https://bugzilla.redhat.com/show_bug.cgi?id=1222365
https://bugzilla.redhat.com/show_bug.cgi?id=1222673
https://bugzilla.redhat.com/show_bug.cgi?id=1228278
https://bugzilla.redhat.com/show_bug.cgi?id=1232510

See also rails/rails#6865
400fe2b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment