0

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

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.