MySQL/Rails中的歧义列查找方法

编程入门 行业动态 更新时间:2024-10-25 10:22:43
本文介绍了MySQL/Rails中的歧义列查找方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到此错误

Mysql :: Error:字段列表中的列"id"不明确

Mysql::Error: Column 'id' in field list is ambiguous

使用类似以下的查找方法时:self.prompts.find(:all, :select => 'id')

when using a find method like such: self.prompts.find(:all, :select => 'id')

正在使用has_many:through关联来调用模型,因此MySQL抱怨存在多个"id"列,因为使用的所有3个表都有一个"id"列.

The models are being called using a has_many :through association, so MySQL complains that there are multiple 'id' columns, since all 3 tables being used have an 'id' column.

我检查了一下,了解了SQL端出了什么问题,但是不知道如何在ActiveRecord查找方法中解决它,并且我对自己的SQL能力不满意,无法尝试滚动自己的SQL查询.有没有一种方法可以将find方法按摩到可以很好发挥作用的东西上?

I looked this up and understand what is going wrong on the SQL end, but don't know how to resolve it in the ActiveRecord find method, and I'm not confident in my SQL abilities to try rolling my own SQL query. Is there a way to massage the find method into something that'll play well?

编辑

以下是相关的Actor模型代码:

Here is the relevant Actor model code:

class Actor < ActiveRecord::Base has_many :acts, :dependent => :destroy has_many :decisions, :through => :acts, :order => 'created_at' has_many :prompts, :through => :decisions, :order => 'id'

推荐答案

您需要更明确地了解要选择的ID.例如:

You need to be more explicit about which id you want to select. For example:

self.prompts.find(:all, :select => 'prompts.id') #prompts is the table name

更多推荐

MySQL/Rails中的歧义列查找方法

本文发布于:2023-11-07 06:44:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565796.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:歧义   方法   MySQL   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!