Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove column restrictions for #count, let the database raise if the SQL is invalid. #10710

Merged

Conversation

senny
Copy link
Member

@senny senny commented May 21, 2013

Fixes #5554

Previously the code path for #count used some validation logic and used to fall back on #count(:all). This prevented the execution of some specific count queries.

This patch gets rid of the validation logic and let's the database decide if the query is valid or not. One backwards incompatible result of this patch is the following:

relation = User.select("id, name")

# later in the code
relation.count

This count will now blow up because it you can't count on two columns. The solution is to specify the counting column explicitly:

relation.count(:all)

@senny
Copy link
Member Author

senny commented May 21, 2013

@jonleighton this is the patch I asked you about. What do you think?

/cc @rafaelfranca @carlosantoniodasilva

@senny
Copy link
Member Author

senny commented May 29, 2013

@neerajdotname maybe you could give me your thoughts on this one?

@neerajsingh0101
Copy link

@senny PR looks good to me. +1.

@jonleighton
Copy link
Member

@senny sorry for my slowness. I am happy to merge this, but it needs a rebase. Also could you expand the changelog message to note that the previous behaviour was untested and surprising, which is why we're making this change. Also would be good to note in changelog that people can do count(:all), as explained in your message above.

@senny
Copy link
Member Author

senny commented Jun 9, 2013

@jonleighton I rebased the PR and extended the CHANGELOG to include the pieces that you mentioned. Can you take another look?

jonleighton added a commit that referenced this pull request Jun 9, 2013
…ounts

Remove column restrictions for `#count`, let the database raise if the SQL is invalid.
@jonleighton jonleighton merged commit f5e133e into rails:master Jun 9, 2013
jonleighton added a commit that referenced this pull request Jun 9, 2013
…ounts

Remove column restrictions for `#count`, let the database raise if the SQL is invalid.
Conflicts:
	activerecord/CHANGELOG.md
jonleighton added a commit that referenced this pull request Jun 9, 2013
…ounts

Remove column restrictions for `#count`, let the database raise if the SQL is invalid.
Conflicts:
	activerecord/CHANGELOG.md
dhh added a commit that referenced this pull request Jun 11, 2013
…ise_on_counts".

This commit causes certain associations to no longer be able to be found through includes (polymorphic belongs_to).

This reverts commit 433e75f.
rafaelfranca added a commit that referenced this pull request Oct 1, 2013
…ise_on_counts"

This reverts commit b8e2978.

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/relation/calculations.rb

Reason: This change is not backward compatible and it was reverted
before 4.0.0 at 2ad168e so we can't include in 4-0-stable.

Closes #12417
nertzy added a commit to Casecommons/pg_search that referenced this pull request Jan 5, 2014
Due to Rails pull request #10710, count without a parameter and empty? don't work with a pg_search relation.

rails/rails#10710
@devkrd
Copy link

devkrd commented Apr 27, 2018

Could you tell me where exactly to change the path to the file, the version of rails 5.0.3

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

Successfully merging this pull request may close these issues.

#count doesn't honor distinct attributes in select clause
7 participants