【问题标题】:Unknown column in field list - rails字段列表中的未知列 - rails
【发布时间】:2013-12-03 10:54:09
【问题描述】:

我得到这个错误:

Mysql2::Error: Unknown column 's.supplier' in 'field list': SELECT s.id, s.supplier, s.vendor, s.title, s.edition, CAST(SUM(l.amount) as UNSIGNED) AS amount
FROM softwares s
LEFT JOIN licenses l ON s.id=l.software_id
GROUP BY s.supplier, s.vendor, s.title, s.edition

但是供应商是软件表中的一列,所以我不确定为什么它不能识别它?

【问题讨论】:

  • 启动控制台(rails c)并粘贴Software的输出
  • 软件(id: integer, vendor: string, title: string, edition: string, description: string, productcode: string, supplier_id: integer, created_at: datetime, updated_at: datetime)
  • 使用supplier_id而不是supplier,请粘贴上面查询的代码

标签: mysql ruby-on-rails database


【解决方案1】:

s.supplier_id,应该是。您使用了错误的列名。

【讨论】:

  • "supplier" 将是 Rails 模型中的关系。在 db-query 中,它必须是“supplier_id”
【解决方案2】:

使用supplier_id 代替supplier

您的查询应该类似于以下内容

SELECT s.id, s.supplier_id, s.vendor, s.title, s.edition, CAST(SUM(l.amount) as UNSIGNED) AS amount
FROM softwares s
LEFT JOIN licenses l ON s.id=l.software_id
GROUP BY s.supplier_id, s.vendor, s.title, s.edition

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 2019-12-09
    • 2023-03-14
    • 2020-10-27
    相关资源
    最近更新 更多