Reputation: 85
I need to get movies with fresh series first.
In rails I have:
l, o = 30, 0
Movie.joins(:series).order("series.created_at DESC").group("movies.id").limit(l).offset(o)
It translate to:
SELECT "movies".*
FROM "movies" INNER JOIN "series" ON "series"."movie_id" = "movies"."id"
GROUP BY movies.id
ORDER BY series.created_at DESC
LIMIT 30 OFFSET 0
But I have error message
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR:
column "series.created_at" must appear in the GROUP BY clause or be used
in an aggregate function
LINE 1: ..._id" = "movies"."id" GROUP BY movies.id ORDER BY series.cre...
^
And OFFSET working very strange, sometimes he duplicate results.
How can I fix this without using sql? Would it work in MySQL?
In Pg I have many little problems like this, how do u think if I change it to MySQL would it work better?
PS: In sqlite it works file
Upvotes: 0
Views: 179
Reputation: 1596
Use agg max if you want fresh one Movie.joins(:series).order("max(series.created_at) DESC").group("movies.id").limit(l).offset(o)
Without aggregation this query does not know what series to order - it s not a pgsql or mysql issue.
In posgres you have more possibilities for aggregations
Upvotes: 3