MySQL/Rails 查找方法中的列不明确

发布于 2024-10-16 15:02:27 字数 639 浏览 3 评论 0原文

我收到这个错误

Mysql::错误:字段列表中的列“id”不明确

self.prompts.find(:all, :select => 'id')

模型是使用 has_many :through 关联调用,因此 MySQL 抱怨有多个“id”列,因为使用的所有 3 个表都有一个“id”列。

我查了一下这个并了解 SQL 端出了什么问题,但不知道如何在 ActiveRecord find 方法中解决它,并且我对自己的 SQL 能力没有信心尝试滚动自己的 SQL 查询。有没有办法将 find 方法调整为可以正常运行的方法?

编辑

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

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

I'm getting this error

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

when using a find method like such: self.prompts.find(:all, :select => '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.

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?

edit

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'

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

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

发布评论

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

评论(1

过期情话 2024-10-23 15:02:27

您需要更明确地了解您要选择哪个 id。例如:

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

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