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

Grouping failing #197

Closed
tobiasgr opened this issue May 4, 2018 · 5 comments
Closed

Grouping failing #197

tobiasgr opened this issue May 4, 2018 · 5 comments

Comments

@tobiasgr
Copy link

tobiasgr commented May 4, 2018

Hi guys

I've run into a strange error here.

I am trying to do this: Lead.group_by_month(:created_at).count

And get this error (maybe its related to the "timestamptz" parts? I have not noticed that before):

PG::GroupingError: ERROR: column "leads.created_at" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...agen')) AT TIME ZONE 'Europe/Copenhagen' ORDER BY "leads"."c...
^
: SELECT COUNT(*) AS count_all, (DATE_TRUNC('month', (created_at::timestamptz) AT TIME ZONE 'Europe/Copenhagen')) AT TIME ZONE 'Europe/Copenhagen' AS date_trunc_month_created_at_timestamptz_at_time_zone_europe_cop FROM "leads" WHERE (created_at IS NOT NULL) GROUP BY (DATE_TRUNC('month', (created_at::timestamptz) AT TIME ZONE 'Europe/Copenhagen')) AT TIME ZONE 'Europe/Copenhagen' ORDER BY "leads"."created_at" DESC

My setup:
Rails 5.1.5
Groupdate 4.0.1
pg 0.21.0
Postgressql: 9.6

@ankane
Copy link
Owner

ankane commented May 4, 2018

Check out #100

@ankane ankane closed this as completed May 4, 2018
@tobiasgr
Copy link
Author

tobiasgr commented May 4, 2018

Thank you!! Indeed worked removing the sorting order :)

@marcamillion
Copy link

marcamillion commented May 8, 2018

I get a similar error, but I have no sorting order on my model.

My models are as follows:

# == Schema Information
#
# Table name: portfolios
#
#  id                   :bigint(8)        not null, primary key
#  user_id              :integer
#  current_dollar_value :float            default(0.0)
#  dollar_change        :float            default(0.0)
#

class Portfolio < ApplicationRecord
  belongs_to :user
  has_many :port_stocks
end

# == Schema Information
#
# Table name: port_stocks
#
#  id             :bigint(8)        not null, primary key
#  portfolio_id   :integer
#  stock_id       :integer
#  volume         :integer
#

class PortStock < ApplicationRecord
  belongs_to :portfolio
  belongs_to :stock
end

I have tried the following, both of which don't work and give me the same error:

<%= pie_chart PortStock.group("portfolio_id") %>

and

<%= pie_chart @portfolio.port_stocks.group(:current_value) %>

This is the error I get:

PG::GroupingError: ERROR:  column "port_stocks.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "port_stocks".* FROM "port_stocks" WHERE "port_stocks...
               ^
: SELECT "port_stocks".* FROM "port_stocks" WHERE "port_stocks"."portfolio_id" = $1 GROUP BY "port_stocks"."current_value"

Or

PG::GroupingError: ERROR:  column "port_stocks.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "port_stocks".* FROM "port_stocks" GROUP BY "port_sto...
               ^
: SELECT "port_stocks".* FROM "port_stocks" GROUP BY "port_stocks"."portfolio_id"

Rails 5.2.0
Ruby 2.5.1
Chartkick 2.3.4
Groupdate 4.0.1

Thoughts?

Edit 1

I also tried the following, after reading #100 :

<%= pie_chart @portfolio.port_stocks.group(:current_value).unscope(:order) %>

But that still produces the following error:

PG::GroupingError: ERROR:  column "port_stocks.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "port_stocks".* FROM "port_stocks" WHERE "port_stocks...
               ^
: SELECT "port_stocks".* FROM "port_stocks" WHERE "port_stocks"."portfolio_id" = $1 GROUP BY "port_stocks"."current_value"

Edit 2

For what it's worth, I tried this:

<%= pie_chart @portfolio.port_stocks.group(:current_value).count %>

And that worked.....I can't say why though.

@marcamillion
Copy link

To address my above comment, I actually figured it out which you can see the resolution on Stack Overflow here: https://stackoverflow.com/questions/50243457/how-do-i-get-associations-working-right-with-chartkick/50245478#50245478

@ankane
Copy link
Owner

ankane commented May 9, 2018

@marcamillion see #166

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

No branches or pull requests

3 participants