ActiveRecord :: StatementInvalid错误

时间:2012-10-21 15:35:46

标签: heroku ruby-on-rails-3.1

这个问题已经被问了很多,但是,似乎没有任何工作。我的应用程序在测试中运行完美,但是当在heroku上运行时,我收到错误500,说“我们很抱歉,但出了点问题。”我检查了服务器的日志文件,我的错误似乎在某处:

2012-10-21T15:29:40+00:00 app[web.1]: 
2012-10-21T15:29:40+00:00 app[web.1]: Started GET "/" for 117.192.23.223 at 2012-10-21 15:29:40 +0000
2012-10-21T15:29:40+00:00 app[web.1]: cache: [GET /] miss
2012-10-21T15:29:40+00:00 app[web.1]: 
2012-10-21T15:29:40+00:00 heroku[router]: GET whispering-coast-7415.herokuapp.com/ dyno=web.1 queue=0 wait=0ms service=5ms status=301 bytes=115
2012-10-21T15:29:40+00:00 app[web.1]: 
2012-10-21T15:29:40+00:00 app[web.1]: Started GET "/movies" for 117.192.23.223 at 2012-10-21 15:29:40 +0000
2012-10-21T15:29:40+00:00 app[web.1]: 
2012-10-21T15:29:40+00:00 app[web.1]:   Processing by MoviesController#index as HTML
2012-10-21T15:29:40+00:00 app[web.1]: Completed 500 Internal Server Error in 2ms
2012-10-21T15:29:40+00:00 heroku[router]: GET whispering-coast-7415.herokuapp.com/movies dyno=web.1 queue=0 wait=0ms service=17ms status=500 bytes=728
2012-10-21T15:29:40+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::Error: ERROR:  column "movies.id" must appear in the GROUP BY clause or be used in an aggregate function
2012-10-21T15:29:40+00:00 app[web.1]: LINE 1: SELECT "movies".* FROM "movies"  GROUP BY rating
2012-10-21T15:29:40+00:00 app[web.1]: 
2012-10-21T15:29:40+00:00 app[web.1]:                ^
2012-10-21T15:29:40+00:00 app[web.1]: 
2012-10-21T15:29:40+00:00 app[web.1]: cache: [GET /movies] miss
2012-10-21T15:29:40+00:00 app[web.1]: : SELECT "movies".* FROM "mo
vies"  GROUP BY rating):
2012-10-21T15:29:40+00:00 app[web.1]:   app/controllers/movies_controller.rb:11:in `index'
2012-10-21T15:29:40+00:00 app[web.1]:   app/models/movie.rb:4:in `get_ratings'
2012-10-21T15:29:40+00:00 app[web.1]: 
2012-10-21T15:29:41+00:00 heroku[router]: GET whispering-coast-7415.herokuapp.com/favicon.ico dyno=web.1 queue=0 wait=0ms service=8ms status=200 bytes=0
2012-10-21T15:29:41+00:00 app[web.1]: cache: [GET /favicon.ico] stale, invalid, store

2012-10-21T15:29:40+00:00 app[web.1]: 2012-10-21T15:29:40+00:00 app[web.1]: Started GET "/" for 117.192.23.223 at 2012-10-21 15:29:40 +0000 2012-10-21T15:29:40+00:00 app[web.1]: cache: [GET /] miss 2012-10-21T15:29:40+00:00 app[web.1]: 2012-10-21T15:29:40+00:00 heroku[router]: GET whispering-coast-7415.herokuapp.com/ dyno=web.1 queue=0 wait=0ms service=5ms status=301 bytes=115 2012-10-21T15:29:40+00:00 app[web.1]: 2012-10-21T15:29:40+00:00 app[web.1]: Started GET "/movies" for 117.192.23.223 at 2012-10-21 15:29:40 +0000 2012-10-21T15:29:40+00:00 app[web.1]: 2012-10-21T15:29:40+00:00 app[web.1]: Processing by MoviesController#index as HTML 2012-10-21T15:29:40+00:00 app[web.1]: Completed 500 Internal Server Error in 2ms 2012-10-21T15:29:40+00:00 heroku[router]: GET whispering-coast-7415.herokuapp.com/movies dyno=web.1 queue=0 wait=0ms service=17ms status=500 bytes=728 2012-10-21T15:29:40+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::Error: ERROR: column "movies.id" must appear in the GROUP BY clause or be used in an aggregate function 2012-10-21T15:29:40+00:00 app[web.1]: LINE 1: SELECT "movies".* FROM "movies" GROUP BY rating 2012-10-21T15:29:40+00:00 app[web.1]: 2012-10-21T15:29:40+00:00 app[web.1]: ^ 2012-10-21T15:29:40+00:00 app[web.1]: 2012-10-21T15:29:40+00:00 app[web.1]: cache: [GET /movies] miss 2012-10-21T15:29:40+00:00 app[web.1]: : SELECT "movies".* FROM "mo vies" GROUP BY rating): 2012-10-21T15:29:40+00:00 app[web.1]: app/controllers/movies_controller.rb:11:in `index' 2012-10-21T15:29:40+00:00 app[web.1]: app/models/movie.rb:4:in `get_ratings' 2012-10-21T15:29:40+00:00 app[web.1]: 2012-10-21T15:29:41+00:00 heroku[router]: GET whispering-coast-7415.herokuapp.com/favicon.ico dyno=web.1 queue=0 wait=0ms service=8ms status=200 bytes=0 2012-10-21T15:29:41+00:00 app[web.1]: cache: [GET /favicon.ico] stale, invalid, store

1 个答案:

答案 0 :(得分:0)

PostgreSQL(在Heroku上)要求你对SELECT列表中的列进行GROUP BY all ,所以在这种情况下代替:

SELECT "movies".* FROM "movies"  GROUP BY rating

您可能希望执行以下操作:

SELECT rating FROM "movies" GROUP BY rating

或在Rails中:

Movie.select(:rating).group(:rating)