我有一个 Ruby on Rails 2.3.x 应用程序,我正在尝试从我自己的 VPS 迁移到 Heroku,包括从 SQLite(开发)和 MySQL(生产)移植到 Postgres。
这是我正在使用的典型 Rails 调用:
spots = Spot.paginate(:all, :include => [:thing, :user, :store, {:thing => :tags}, {:thing => :brand}], :group => :thing_id, :order => order, :conditions => conditions, :page => page, :per_page => per_page)
问题 1:我收到很多错误,例如PG::Error: ERROR: column "spots.id" must appear in the GROUP BY clause or be used in an aggregate function
. SQLite/MySQL 显然在这里更宽容。当然,我可以通过将指定字段添加到我的:group
参数来轻松解决这些问题,但我觉得我的代码搞砸了。有没有更好的办法?
问题 2:如果我输入 Postgres 缺少的所有 GROUP BY 列,我最终会得到以下语句(仅:group
已更改):
spots = Spot.paginate(:all, :include => [:thing, :user, :store, {:thing => :tags}, {:thing => :brand}], :group => 'thing_id,things.id,users.id,spots.id', :order => order, :conditions => conditions, :page => page, :per_page => per_page)
这又会产生以下 SQL 代码:
SELECT * FROM (SELECT DISTINCT ON ("spots".id) "spots".id, spots.created_at AS alias_0 FROM "spots"
LEFT OUTER JOIN "things" ON "things".id = "spots".thing_id
WHERE (spots.recommended_to_user_id = 1 OR spots.user_id IN (1) OR things.is_featured = 't')
GROUP BY thing_id,things.id,users.id,spots.id) AS id_list
ORDER BY id_list.alias_0 DESC LIMIT 16 OFFSET 0;
...产生错误PG::Error: ERROR: missing FROM-clause entry for table "users"
。我该如何解决这个问题?