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

Add Relation#unordered #400

Merged
merged 2 commits into from
Mar 19, 2021
Merged

Add Relation#unordered #400

merged 2 commits into from
Mar 19, 2021

Conversation

radar
Copy link
Contributor

@radar radar commented Mar 16, 2021

Uses Sequel's built-in unordered to remove the order clause from queries.

Today I was trying to do this query:

select { [integer::count(id).as(:count), user_id, project_id] }.group_and_count(:project_id, :user_id)

But I was told I could not do that:

[30] pry(#<TicketsRelation>)> select { [integer::count(id).as(:count), user_id, project_id] }.group_and_count(:project_id, :user_id).to_a
  ROM[postgres] (0.8ms)  SELECT "project_id", "user_id", count(*) AS "count" FROM "tickets" GROUP BY "project_id", "user_id" ORDER BY "tickets"."id"
Sequel::DatabaseError: PG::GroupingError: ERROR:  column "tickets.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...ickets" GROUP BY "project_id", "user_id" ORDER BY "tickets"....

This is because datasets are ordered by id by default.

After some googling, I found Sequel provides unordered, but rom-sql did not. Seemingly, the way to do it in rom-sql is order(nil):

select { [integer::count(id).as(:count), user_id, project_id] }.order(nil).group_and_count(:project_id, :user_id)

I think unordered is a clearer API for just a few lines of code, and it'll match what Sequel provides.

select { [integer::count(id).as(:count), user_id, project_id] }.unordered.group_and_count(:project_id, :user_id)

@radar radar requested a review from solnic as a code owner March 16, 2021 03:18
@solnic
Copy link
Member

solnic commented Mar 18, 2021

@radar thanks for the PR - could you rebase it on top of release-3.3 branch and change the base branch of this PR to release-3.3? This way I'll be able to easily release and then update master branch.

@radar radar changed the base branch from master to release-3.3 March 18, 2021 21:48
radar added 2 commits March 19, 2021 08:50
Uses Sequel's built-in unordered to remove the order clause from queries
@radar
Copy link
Contributor Author

radar commented Mar 18, 2021

@solnic all done

@solnic solnic merged commit 4b41f52 into rom-rb:release-3.3 Mar 19, 2021
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.

2 participants