Mysql2::错误:列“created_at” in order 子句不明确

发布于 2024-11-05 05:51:37 字数 1035 浏览 0 评论 0原文

我有以下运行正常的查询

ObjectItem.find(:all, :include => :object_groups, :conditions =>  "object_items.description LIKE '%#{search}%' OR object_groups.description LIKE '%#{search}%'", :order => 'object_items.created_at DESC')

但是现在不推荐使用这种方式进行查询,因此我尝试更改为这种形式

ObjectItem.order('object_items.created_at DESC').includes(:object_groups).where("object_items.description LIKE '%#{search}%' OR object_groups.description LIKE '%#{search}%'")

但我收到以下错误:

Mysql2::Error: Column created_at in order clause is ambiguous: SELECT  DISTINCT `object_items`.id FROM `object_items` LEFT OUTER JOIN `object_groups_object_items` ON `object_groups_object_items`.`object_item_id` = `object_items`.`id` LEFT OUTER JOIN `object_groups` ON `object_groups`.`id` = `object_groups_object_items`.`object_group_id` WHERE (object_items.description LIKE '%%' OR object_groups.description LIKE '%%') ORDER BY object_items.created_at DESC, created_at DESC  LIMIT 20 OFFSET 0

I have the following query that is working correctly

ObjectItem.find(:all, :include => :object_groups, :conditions =>  "object_items.description LIKE '%#{search}%' OR object_groups.description LIKE '%#{search}%'", :order => 'object_items.created_at DESC')

But doing query this way is now deprecated so i'm trying to change to this form

ObjectItem.order('object_items.created_at DESC').includes(:object_groups).where("object_items.description LIKE '%#{search}%' OR object_groups.description LIKE '%#{search}%'")

But i'm getting the following error:

Mysql2::Error: Column created_at in order clause is ambiguous: SELECT  DISTINCT `object_items`.id FROM `object_items` LEFT OUTER JOIN `object_groups_object_items` ON `object_groups_object_items`.`object_item_id` = `object_items`.`id` LEFT OUTER JOIN `object_groups` ON `object_groups`.`id` = `object_groups_object_items`.`object_group_id` WHERE (object_items.description LIKE '%%' OR object_groups.description LIKE '%%') ORDER BY object_items.created_at DESC, created_at DESC  LIMIT 20 OFFSET 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

泅渡 2024-11-12 05:51:37

order('users.created_at DESC')

基本上问题是可能存在联接查询,因此两个表都有created_at字段,因此它会抛出错误。
如果您想根据创建的表对结果进行排序,请使用您对结果进行排序的 table_name.field_name 。

order('users.created_at DESC')

Basically the problem is that there is join query probably so both the table has the created_at field so its throw the error.
if you want to order the result according to which tables created_at so using the table_name.field_name on which you order the result.

说好的呢 2024-11-12 05:51:37
ORDER BY object_items.created_at DESC, created_at DESC LIMIT 20 OFFSET 0

正如前面的答案所说,它有一个悬挂的“created_at”,可以引用任一表中的列。

它显然不是您直接通过 .order 传递的 - 所以也许您的模型中某处有一个带有默认顺序的默认范围。
如果您明确地创建 object_items.created_at 它可能会解决问题。

ORDER BY object_items.created_at DESC, created_at DESC LIMIT 20 OFFSET 0

As the previous answer says, it's got a hanging "created_at" that could refer to the column in either table.

It's clearly not the one that you are directly passing in with .order - so perhaps you have a default scope with a default order somewhere in your model.
If you explicitly make that object_items.created_at it may possibly fix the problem.

伴我心暖 2024-11-12 05:51:37

试试这个:
order('users.created_at DESC')
或者
order(created_at: :asc)

应该用于同一件事

try this :
order('users.created_at DESC')
or
order(created_at: :asc)

should be worked for the same thing

小…红帽 2024-11-12 05:51:37

这通常是同一字段名称出现在多个表中的结果。

在您的情况下,object_items 和 object_groups 看起来都有相同的字段“created_at”。

最好的事情(在我看来)是通过(例如)使用字段名称的前缀来重命名其中一个表中的字段。

This is usually the result of the same field name appearing in multiple tables.

In your case it looks like object_items and object_groups both have the same field 'created_at'.

The best thing (in my opinion) would be to rename the field in one of the tables by (for example) using a prefix to the field name.

最近可好 2024-11-12 05:51:37

您是如何在 ObjectItem 模型中定义 object_groups 关系的?

如果您在 ObjectItem 中具有类似于以下的关系,则可以将 ORDER BYcreated_ar DESC 添加到查询中:

has_many :object_groups, :order => "created_at DESC"

How did you define the object_groups relation in ObjectItem model?

ORDER BY created_ar DESC may be added to the query if you have relation similar to following in ObjectItem:

has_many :object_groups, :order => "created_at DESC"
贵在坚持 2024-11-12 05:51:37

我今天在 Spree 项目中遇到了这个问题。这几乎总是由于将表名从 order 子句中删除而引起的。请参阅此票证以了解我如何解决问题: https://github.com/spree/spree/issues /2654

就我而言,我向我的狂欢模型之一添加了一个装饰器,并且它添加了一个没有适当表名的订单子句。

I ran into this today on a Spree project. It is almost always caused by leaving the table name off the order clause. See this ticket for how I solved the problem: https://github.com/spree/spree/issues/2654

In my case, I had added a decorator to one of my spree models, and it was adding an order clause without the appropriate table name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文