0

我的一个模型有一个命名范围,可以正常工作。代码是:

named_scope :inbox_threads, lambda { |user|
{
  :include => [:deletion_flags, :recipiences],
  :conditions => ["recipiences.user_id = ? AND deletion_flags.user_id IS NULL", user.id],
  :group => "msg_threads.id"
}}

这在使用 MySQL 数据库的应用程序的本地副本上运行良好,但是当我将应用程序推送到 Heroku(仅使用 PostgreSQL)时,出现以下错误:

ActiveRecord::StatementInvalid (PGError: ERROR: column "msg_threads.subject" must appear in the GROUP BY clause or be used in an aggregate function:

SELECT "msg_threads"."id" AS t0_r0, "msg_threads"."subject" AS t0_r1, "msg_threads"."originator_id" AS t0_r2, "msg_thr eads"."created_at" AS t0_r3, "msg_threads"."updated_at" AS t0_r4, "msg_threads"."url_key" AS t0_r5, "deletion_flags"."id" AS t1_r0, "deletion_flags"."user_id" AS t1_r1, "deletion_flags"."msg_thread_id" AS t1_r2, "deletion_flags"."confirmed" AS t1_r3, "deletion_flags"."created_at" AS t1_r4, "deletion_flags"."updated_at" AS t1_r5, "recipiences"."id" AS t2_r0, "recipiences"."user_id" AS t2_r1, "recipiences"."msg_thread_id" AS t2_r2, "recipiences"."created_at" AS t2_r3, "recipien ces"."updated_at" AS t2_r4 FROM "msg_threads" LEFT OUTER JOIN "deletion_flags" ON deletion_flags.msg_thread_id = msg_threads.id LEFT OUTER JOIN "recipiences" ON recipiences.msg_thread_id = msg_threads.id WHERE (recipiences.user_id = 1 AND deletion_flags.user_id IS NULL) GROUP BY msg_threads.id)

我不熟悉 Postgres 的工作,所以我需要在此处添加什么才能使其正常工作?谢谢!

4

1 回答 1

1

... must appear in the GROUP BY clause ...

MySQL 公然违反标准以支持简单性,并允许您不指定要查找的每一列GROUP BY,而是依靠排序来选择“最佳”可用值。

另一方面,Postgres(和大多数其他 RDBMS)要求您命名您在GROUP BY子句中选择的每一列。如果您手动组装了该查询,则需要将每一列一一添加到GROUP BY. 如果 ORM 生成了该查询(这是基于所有双引号的样子),您将需要检查他们的 Postgres 支持和/或向他们提交错误。

于 2010-06-15T05:31:16.493 回答