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

Qualify column names in calculation #21756

Closed
wants to merge 5 commits into from

Conversation

soutaro
Copy link
Contributor

@soutaro soutaro commented Sep 24, 2015

This is to qualify column names inserted by group in calculation query.

A calculation including joins and group like the following may cause a SQL error.

AuditLogs.joins(:developer).group(:id).count

Here, column name given to group call also exists in the joined table. The column name given via group will not qualified with table names, and the result is an error.

ActiveRecord::StatementInvalid: SQLite3::SQLException: ambiguous column name: id: SELECT COUNT(*) AS count_all, id AS id FROM "audit_logs" INNER JOIN "developers" ON "developers"."id" = "audit_logs"."developer_id" GROUP BY "audit_logs"."id"

This patch is to fix this by qualifying the column names with table name.

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @matthewd (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@soutaro
Copy link
Contributor Author

soutaro commented Sep 24, 2015

The last CI failure looks like because master is failing...

@@ -102,6 +102,12 @@ def test_should_group_by_summed_field
assert_equal 60, c[2]
end

def test_should_qualify_fields_by_group
assert_nothing_raised do
AuditLog.joins(:developer).group(:id).count
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check the value to make sure that this is doing the right thing? Not raising an exception doesn't mean that there isn't necessarily some issue

@sgrif
Copy link
Contributor

sgrif commented Sep 24, 2015

I've restarted the build for you. Master is passing now. Other than that one comment this looks fine. Please squash your commits and add a changelog entry.

@sgrif sgrif assigned sgrif and unassigned matthewd Sep 24, 2015
@sgrif
Copy link
Contributor

sgrif commented Sep 24, 2015

Ok now master is passing. It's been a long afternoon. >_>

@soutaro soutaro force-pushed the qualify-column-in-calculation branch from 888b5a1 to 8f41355 Compare September 25, 2015 17:35
end
end

def test_should_calculate_against_given_relation
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not very sure which test I should add. Is this ok?

@soutaro
Copy link
Contributor Author

soutaro commented Sep 25, 2015

Added test and updated CHANGELOG, and rebased because CHANGELOG update causes a conflict.
If this is good to merge, I will squash the commits.

@sgrif
Copy link
Contributor

sgrif commented Oct 20, 2015

Squashed and merged manually in 03aacc5

@sgrif sgrif closed this Oct 20, 2015
sgrif added a commit that referenced this pull request Oct 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants