0
votes

I've recently migrated my database from sqlite3 to postgreSQL for a rails app I've been working on. On one of my pages, I'm rendering a graph using "ChartKick". Prior to switching to postgreSQL my table and graph were functioning fine. Now the page fails and I get an error:

PG::GroupingError: ERROR: column "stats.created_at" must appear in the >GROUP BY clause or be used in an aggregate function

I've used the Google and tried troubleshooting it a bit but cannot get my graph to function properly.

"<%= line_chart @stats.group(:date).sum(:weight) %>"

PG::GroupingError: ERROR: column "stats.created_at" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ..."."user_id" = $1 GROUP BY "stats"."date" ORDER BY created_at... ^ : SELECT SUM("stats"."weight") AS sum_weight, "stats"."date" AS stats_date FROM "stats" WHERE "stats"."user_id" = $1 GROUP BY "stats"."date" ORDER BY created_at DESC LIMIT $2 OFFSET $3

EDIT Heres what I have in my controler to query the data:

# GET /stats.json def index @stats = current_user.stats.paginate(:page => params[:page], per_page: 5 ).order('created_at DESC')
end

I'm also using the Ruby Gem "Paginate" for my table.

1
It's a PostgreSQL error, not a ChartKick error (that's why it says PG::GroupingError::ERROR). Perhaps it would help to show the query code.jvillian
It's probably coming from this line @stats.group(:date).sum(:weight). What's the stats table look like? Is date the name of that database column not created_at?SomeSchmo

1 Answers

0
votes

SOLVED:

Postgres wasn't able to group the data because my code did not specify what data to get. by adding "current_user" to my ChartKick code, it was able to SELECT and render the data fine.

would fail

"<%= line_chart @stats.group(:date).sum(:weight) %>"<

Works!

"<%= line_chart current_user.stats.group(:date).sum(:weight), discrete: true %>"