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

Fixed PG::InvalidColumnReference with DISTINCT & by_distance #127

Closed
wants to merge 1 commit into from

Conversation

elsurudo
Copy link

I am using PG and Rails 5.1.

I had a pretty complex statement that used both .distinct and the .by_distance scope, and I was getting this error:

PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list

I dug down a bit and found that it was happening because the formula was right in the ORDER BY clause, and Postgres was having none of it.

This PR is a fix that worked for me. Whether it's something should be merged in, I don't know. But it did fix a bug for me, and it may for others, as well. If there is some further refactoring I could do to get this merged, let me know.

Basically, I am looking for feedback.


distance_formula = distance_sql(origin, units, formula).gsub(/\s+/, '')
with_latlng.select("#{arel.quoted_table_name}.*", "#{distance_formula} AS #{distance_column_name}")
.order("#{distance_column_name} #{options[:reverse] ? 'DESC' : 'ASC'}")

Choose a reason for hiding this comment

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

Place the . on the previous line, together with the method call receiver.
Line is too long. [90/80]

arel = self.is_a?(ActiveRecord::Relation) ? self : self.all

distance_formula = distance_sql(origin, units, formula).gsub(/\s+/, '')
with_latlng.select("#{arel.quoted_table_name}.*", "#{distance_formula} AS #{distance_column_name}")

Choose a reason for hiding this comment

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

Line is too long. [107/80]


arel = self.is_a?(ActiveRecord::Relation) ? self : self.all

distance_formula = distance_sql(origin, units, formula).gsub(/\s+/, '')

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

distance_column_name = distance_sql(origin, units, formula)
with_latlng.order("#{distance_column_name} #{options[:reverse] ? 'DESC' : 'ASC'}")

arel = self.is_a?(ActiveRecord::Relation) ? self : self.all

Choose a reason for hiding this comment

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

Redundant self detected.

@coveralls
Copy link

coveralls commented Oct 12, 2017

Coverage Status

Coverage decreased (-0.2%) to 48.617% when pulling b63b3d9 on elsurudo:order-by-fix into 9e5251a on geokit:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.2%) to 48.617% when pulling b63b3d9 on elsurudo:order-by-fix into 9e5251a on geokit:master.

@elsurudo
Copy link
Author

Looks like self.all is a problem because it returns an array instead of a relation. Confused because scoped was removed from Rails. I'm sure there is a way to fix this.

@mnoack
Copy link
Member

mnoack commented Apr 17, 2018

I'm closing until we fix this. But I'm absolutely happy to merge Pull Requests which pass the tests, maintains (or improves test coverage) and the code itself seems reasonable.

@mnoack mnoack closed this Apr 17, 2018
@elsurudo
Copy link
Author

It's been a long time since I created this so I forgot much of the context. Your main objection, in that case, is that it decreased the test coverage, right?

@mnoack
Copy link
Member

mnoack commented Apr 18, 2018

Yes, I generally have 2 rules, it must pass the tests, and it should test the code that is changing in most cases. Given coverage has dropped I presume the entire method is untested.

I would at least like people proposing changing code to attempt to test their change. Because in future someone might change this code and break your setup and we won't know until we do a release and then it's too late.

Finally the gsub doesn't appear to be explained as to why it's needed. It would be great to explain this (it's good to do things in separate commits if they are different). The other question would be should we fix the gsub within the distance_sql method because if we call it in other places it would be wrong then wouldn't it? (because other callers will forget to gsub)

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