【问题标题】:Group by no longer works when switching to MS Sql Server / TinyTds切换到 MS Sql Server / TinyTds 时,Group by 不再起作用
【发布时间】:2016-03-10 21:26:04
【问题描述】:

我刚刚将一个 Rails 应用程序从 SQLLite 迁移到 MS Sql Server。我有一个小组在 SqlLite 下工作正常,但在使用 SQL / TinyTds 时出现如下错误。

ActiveRecord::StatementInvalid in Complaints#report_by_product
Showing C:/Users/cmendla/RubymineProjects/internal_complaints/app/views/complaints/report_by_product.html.erb where line #10 raised:

TinyTds::Error: Column 'ic.complaints.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.: EXEC sp_executesql N'SELECT [ic].[complaints].* FROM [ic].[complaints] GROUP BY [ic].[complaints].[product_name]'
Rails.root: C:/Users/cmendla/RubymineProjects/internal_complaints

Application Trace | Framework Trace | Full Trace
app/views/complaints/report_by_product.html.erb:10:in `_app_views_complaints_report_by_product_html_erb___717152080_78192168'
app/controllers/complaints_controller.rb:18:in `report_by_product'

投诉控制器中的副产品定义是

def report_by_product

@complaints = Complaint.all
@complaints_group = Complaint.group("product_name")         # causes the report to be grouped by the product

respond_to do |format|
  format.html
  format.json { render json: @complaints }
end

结束

副产品视图的一部分是

  <tr>
    <% @complaints_group.each do |complaint_group| %>

        <% $complaint_group_name = complaint_group.product_name %>
    <td>
      <%= complaint_group.product_name %>
    </td>
    <td style="text-align: right;">
      <%= (Complaint.where("product_name like ?", $complaint_group_name )).count %>
    </td>
    <td style="text-align: right;" >
      <%= (Complaint.where("product_name like ?
                            AND complaints.created_at > ?",
                           $complaint_group_name, Date.today-360).count) %>
    </td>
        <td style="text-align: right;">
          <%= (Complaint.where("product_name like ?
                            AND complaints.created_at > ?
                            AND complaints.created_at < ? ",
                               $complaint_group_name,
                               Date.today-90,
                               Date.today).count) %>

我的 TDS 宝石是

Installing activerecord-sqlserver-adapter 4.2.8 (was 4.2.4)
Using tiny_tds 0.7.0

【问题讨论】:

    标签: ruby-on-rails tiny-tds


    【解决方案1】:

    我得到了这个工作。我不得不更改控制器中的分组语句。

    来自:

     @complaints_group = Complaint.group("product_name")         
    

    收件人:

       @complaints_group = Complaint.select("product_name").group("product_name")
    

    这让它正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-06
      • 2011-04-18
      • 2023-01-24
      • 2017-12-23
      • 2017-08-08
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      相关资源
      最近更新 更多