Mysql到Postgresql Rails PG :: GroupingError:错误:

时间:2016-06-05 21:12:02

标签: mysql ruby-on-rails postgresql

我刚部署了我的应用程序,不得不从mysql切换到postgresql。 现在执行我的查询时出现以下错误:

ActiveRecord::StatementInvalid (PG::GroupingError: ERROR:  column "search_terms.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "search_terms".* FROM "search_terms" WHERE (search_te...
               ^
: SELECT "search_terms".* FROM "search_terms" WHERE (search_terms.term like '%%') GROUP BY search_terms.game_id):

我有一个名为search_terms的表term:stringgame_id:integer。游戏可以有多个术语。所以我想将game_id匹配的术语分组。

有谁知道如何解决这个问题?

在我的模型search_term.rb中,我定义了此搜索功能:

# Search Terms
  def self.search(query)
    where("search_terms.term like ?", "%#{query}%").group("search_terms.game_id")
  end

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我认为这是你需要尝试的。

 def self.search(query)
    where("search_terms.term like ?", "%#{query}%").group("search_terms.game_id", "search_terms.term")
  end

此处的解释:Rails 3.1 with PostgreSQL: GROUP BY must be used in an aggregate function