DISTINCT is prepended to the count expression when the relation includes or eager_loads #14009

Closed
bughit opened this Issue Feb 11, 2014 · 8 comments

Comments

Projects
None yet
5 participants
Contributor

bughit commented Feb 11, 2014

DISTINCT should only be added if you are counting rows (no argument to count), prepending it to arbitrary expressions will produce unintended nonsense expressions

senny added the activerecord label Feb 11, 2014

Member

senny commented Feb 11, 2014

@bughit please provide an executable test case to reproduce the issue. You can use this script as a foundation. Such a script should be attached to every Active Record issue. It helps a great deal to communicate the problem.

Member

senny commented Mar 8, 2014

It's been a month without feedback. If this is still an issue, please provide a test-case to reproduce so we can investigate further.

On a side note, this might be related to the revert in #14226

senny closed this Mar 8, 2014

Contributor

bughit commented Mar 8, 2014

unless File.exist?('Gemfile')
  File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
  GEMFILE

  system 'bundle'
end

require 'bundler'
Bundler.setup(:default)

require 'active_record'
require 'minitest/autorun'
require 'logger'

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts do |t|
    t.integer :flag
  end

  create_table :comments do |t|
    t.integer :post_id
  end
end

class Post < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

class BugTest < Minitest::Test
  def test_association_stuff
    Post.create! [
      {flag: 1},
      {flag: 1},
      {flag: 0}
    ]

    assert_equal 2, Post.count('flag or null')
    assert_equal 2, Post.eager_load(:comments).count('flag or null')
    assert_equal 2, Post.includes(:comments).count('flag or null')

  end
end

generates

SELECT COUNT(DISTINCT flag or null) FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id"
Member

senny commented Mar 8, 2014

thanks for the reproduction.

senny reopened this Mar 8, 2014

Member

senny commented Mar 8, 2014

@tenderlove is this caused by the same part that was messing with includes in #14226?

Owner

tenderlove commented Mar 8, 2014

@senny sure looks like it, but since @bughit provided a script, you should be able to git bisect (after you remove all the Gemfile preamble junk)

robin850 removed the needs feedback label Mar 8, 2014

rails-bot added the stale label Aug 19, 2014

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

rails-bot closed this Nov 19, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment