MySQL 到 PostreSQL 和命名范围
我的一个模型有一个命名范围,运行良好。代码是:
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:错误:列“msg_threads.subject”必须出现在 GROUP BY 子句中或在聚合函数中使用:
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,"删除_标志”。“msg_thread_id”AS t1_r2,“删除_标志”。“已确认” 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,“收件人”。“创建时间” AS t2_r3,“收件人 ces"."updated_at" AS t2_r4 FROM "msg_threads" LEFT OUTER JOIN "deletion_flags" ONdeletion_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 delete_flags.user_id IS NULL) GROUP BY msg_threads.id)
我对 Postgres 的工作不太熟悉,那么我需要在此处添加什么才能使其正常工作?谢谢!
I've got a named scope for one of my models that works fine. The code is:
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"
}}
This works fine on my local copy of the app with a MySQL database, but when I push my app to Heroku (which only uses PostgreSQL), I get the following error:
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)
I'm not as familiar with the working of Postgres, so what would I need to add here to get this working? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MySQL 公然违反了为了简单起见的标准,并允许您不指定在
GROUP BY< 中查找的每一列/code>,而不是依靠排序来选择“最佳”可用值。
另一方面,Postgres(以及大多数其他 RDBMS)要求您对在 GROUP BY 子句中选择的每一列进行命名。如果您已手动组装该查询,则需要将每一列逐一添加到
GROUP BY
中。如果 ORM 生成了该查询(这就是基于所有双引号的查询),您将需要检查他们的 Postgres 支持和/或向他们提交错误。MySQL flagrantly violates the standard in favor of simplicity and allows you to not specify every single column you're looking for in
GROUP BY
, instead relying on ordering to pick the "best" available value.Postgres (and most other RDBMSes), on the other hand, requires that you name every single column that you're selecting in the
GROUP BY
clause. If you've manually assembled that query, you're going to need to, one by one, add each column intoGROUP BY
. If an ORM generated that query (which is what it looks like based on all the double quotes), you're going to need to check up on their Postgres support and/or file a bug with them.