SQL 语句在 PGAdmin 中有效,但在转换为 Arel 后在应用程序中无效

NoYellAtmonkeys

我在 PGAdmin 中写了这个 sql 语句,它按预期提取数据。

SELECT Sum(timerecords.manual_input_hours)           AS hours,
       To_date(timerecords.time_start, 'YYYY-MM-DD') AS date,
       tasks.title,
       timerecords.client_id
FROM   timerecords
       INNER JOIN tasks
               ON tasks.id = timerecords.task_id
WHERE  timerecords.client_id = '15'
GROUP  BY To_date(timerecords.time_start, 'YYYY-MM-DD'),
          timerecords.task_id,
          timerecords.client_id,
          tasks.title
ORDER  BY date DESC 

然后我在 scuttle.io 中运行 sql 语句转换为 Arel

Timerecord.select(
  [
    Arel::Nodes::NamedFunction.new(
      'SUM', [Timerecord.arel_table[:manual_input_hours]]
    ).as('hours'), Arel::Nodes::NamedFunction.new(
      'TO_DATE', [Timerecord.arel_table[:time_start], 'YYYY-MM-DD']
    ).as('date'), Task.arel_table[:title], Timerecord.arel_table[:client_id]
  ]
).where(Timerecord.arel_table[:client_id].eq('15')).joins(
  Timerecord.arel_table.join(Task.arel_table).on(
    Task.arel_table[:id].eq(Timerecord.arel_table[:task_id])
  ).join_sources
).order(:date).reverse_order.group(
  Arel::Nodes::NamedFunction.new(
    'TO_DATE', [Timerecord.arel_table[:time_start], 'YYYY-MM-DD']
  ), Timerecord.arel_table[:task_id], Timerecord.arel_table[:client_id], Task.arel_table[:title]
)

当我调用@tasks.each.do(在该行)时,我收到此错误消息,但没有其他信息。

Arel::Visitors::UnsupportedVisitError in Agency::Clientmanagement#show
Unsupported argument type: String. Construct an Arel node instead.

我从研究中尝试过的一些项目......而不是to_date,我尝试了to_char。我还将我的开发数据库从 sqllite3 更改为 postgresql 以匹配生产服务器。

最大限度

生成的代码非常复杂,因为它使用 Arel 来处理通过 ActiveRecord 查询接口很容易做到的事情:

date_fn = Arel::Nodes::NamedFunction.new(
  'TO_DATE', [Timerecord.arel_table[:time_start], Arel::Nodes.build_quoted('YYYY-MM-DD')]
)

Timerecord.select(
  tbl[:manual_input_hours].sum.as('hours'), # you don't need to define the function
  date_fn.as('date'), 
  :title,
  :client_id
)  
  .where(client_id: '15')
  .joins(:tasks)
  .order(date: :desc)
  .group(
     :date, :task_id, :client_id, Task.arel_table[:title]
  )

某些数据库(如 Oracle)不允许您在 GROUP BY 子句中使用选定的列。在这种情况下,您需要重复函数调用:

.group(
  date_fn, :task_id, :client_id, Task.arel_table[:title]
)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将伪代码转换为有效的SQL语句

与Arel中的SQL WITH RECURSIVE等效

“ with”语句在Windows中有效,但在Ubuntu中不起作用

为什么我的 sql 语句在我的编辑器中有效,而在我的 JS 函数中无效?

查询在 SQL Server 中有效,但在 R 中无效

SQL 查询在 navicat 中有效,但在 vb.net 中无效

SQL 查询在 access 中有效,但在 excel 中无效

FFmpeg转换为JPEG在VLC中有效,但在Mac Preview中不起作用?

SQL中的SELECT语句有效,但PLSQL中的SELECT INTO语句无效

laravel-permission:assignRole 在 Tinker 中有效,但在应用程序中无效

Firestore规则get()在模拟器中有效,但在应用程序中无效

Python请求在控制台中有效,但在应用程序中无效

为什么此创建表SQL在MSSQL Server中有效但在MySQL上无效?

Ruby:将Arel.sql转换为Arel :: Nodes :: Case的想法?

如何使用工具PGADMIN在Postgres中运行多个sql语句?

在Java中将UTC转换为IST时间在LOCAL中有效,但在CLOUD SERVER中无效

为什么 SHAPE.SDO_ORDINATES(1) 在 PL/SQL 中有效,但在 SQL 中无效?

SQL SELECT WHERE语句有效,但预处理语句无效吗?

使用左联接查询并选择中的计数在MySQL中有效,但在MS SQL Server 2012中无效

查询在查询设计器中有效,但在MS SQL Server报表生成器中无效

此查询实际上在 C# 中有效,但在执行时在 SQL Server 中无效

為什麼投射 SQL MAX(DateTime) 命令在 SSMS 中有效,但在 C# 中無效?

应用程序登录在 chrome 中有效,但在设备上无效

应用程序在开发中有效,但在部署到heroku时无效

导航在浏览器中有效,但在Ionic View iPhone应用程序中无效

Firestore数据库规则在模拟器中有效,但在应用程序中无效

如何在 SQL 中以最有效的方式应用嵌套 case when 语句?

查詢在 SQLite3 中有效,但在 Microsoft SQL Server 中是語法錯誤

RabbitMQ CreateConneciton问题-在一个应用程序中有效,但在另一个应用程序中无效