Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[PostgreSQL] Remove NULLS FIRST/LAST. Closes #5152 #5168

Merged
merged 1 commit into from

2 participants

Toshinori Kajihara Aaron Patterson
Toshinori Kajihara
Collaborator

When we use the postgresql adapter and construct distinct select clause, we have a problem with NULLS first/last.

without fix
=> posts.updater_idnulls first AS alias_0
with fix
=> posts.updater_id AS alias_0

closes #5152

This original issue was reported for 3-2-stable.

Aaron Patterson tenderlove merged commit 699e460 into from
Toshinori Kajihara kennyj referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Toshinori Kajihara kennyj referenced this pull request from a commit in kennyj/rails
Toshinori Kajihara kennyj Backport #5168 to 3-2-stable. Fix a problem that NULLS is ignored by …
…postgresql_adapter.rb while creating distincts.
f137c2b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 25, 2012
  1. Toshinori Kajihara
This page is out of date. Refresh to see the latest.
2  activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
View
@@ -1189,7 +1189,7 @@ def distinct(columns, orders) #:nodoc:
# Construct a clean list of column names from the ORDER BY clause, removing
# any ASC/DESC modifiers
- order_columns = orders.collect { |s| s.gsub(/\s+(ASC|DESC)\s*/i, '') }
+ order_columns = orders.collect { |s| s.gsub(/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i, '') }
order_columns.delete_if { |c| c.blank? }
order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }
5 activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
View
@@ -185,6 +185,11 @@ def test_partial_index
assert_equal "(number > 100)", index.where
end
+ def test_distinct_with_nulls
+ assert_equal "DISTINCT posts.title, posts.updater_id AS alias_0", @connection.distinct("posts.title", ["posts.updater_id desc nulls first"])
+ assert_equal "DISTINCT posts.title, posts.updater_id AS alias_0", @connection.distinct("posts.title", ["posts.updater_id desc nulls last"])
+ end
+
private
def insert(ctx, data)
binds = data.map { |name, value|
Something went wrong with that request. Please try again.