轨道3,ActiveRecord的,PostgreSQL的 - " .uniq"命令不起作用? [英] Rails 3, ActiveRecord, PostgreSQL - ".uniq" command doesn't work?

查看:210
本文介绍了轨道3,ActiveRecord的,PostgreSQL的 - " .uniq"命令不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询:

Article.joins(:themes => [:users]).where(["articles.user_id != ?", current_user.id]).order("Random()").limit(15).uniq

和给我的错误

PG::Error: ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 1: ...s"."user_id" WHERE (articles.user_id != 1) ORDER BY Random() L...

在我的更新的原始查询到

Article.joins(:themes => [:users]).where(["articles.user_id != ?", current_user.id]).order("Random()").limit(15)#.uniq

这样的错误走了......在MySQL中的 .uniq 的作品,在PostgreSQL里没有。存在什么选择吗?

so the error is gone... In MySQL .uniq works, in PostgreSQL not. Exist any alternative?

推荐答案

由于错误状态的SELECT DISTINCT,ORDER BY EX pressions必须出现在选择列表 。 因此,必须明确选择您所订购的条款。

As the error states for SELECT DISTINCT, ORDER BY expressions must appear in select list. Therefore, you must explicitly select for the clause you are ordering by.

下面是一个例子,它类似于你的情况,但概括了一下。

Here is an example, it is similar to your case but generalize a bit.

Article.select('articles.*, RANDOM()')
       .joins(:users)
       .where(:column => 'whatever')
       .order('Random()')
       .uniq
       .limit(15)

所以,明确包括你的 ORDER BY 子句(在这种情况下, RANDOM())使用。选择()。如上图所示,为了让您的查询返回的文章属性,必须明确选择他们也。

So, explicitly include your ORDER BY clause (in this case RANDOM()) using .select(). As shown above, in order for your query to return the Article attributes, you must explicitly select them also.

我希望这可以帮助;好运气

I hope this helps; good luck

这篇关于轨道3,ActiveRecord的,PostgreSQL的 - " .uniq"命令不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆