【问题标题】:ERROR: operator does not exist: timestamp without time zone > integer)错误:运算符不存在:没有时区的时间戳>整数)
【发布时间】:2019-05-08 00:25:09
【问题描述】:

我正在使用 Rails 5.2 并传递一个日期参数。为了简化我的示例,请看下面的 sql 查询,它类似但简化了,因为它太复杂而无法与活动记录方法一起使用,所以我需要运行原始 sql..

sql = 'Select * FROM mytable WHERE created_at > #{@start_date_time}'

1) 当我将参数传递给字符串或执行命令时,如何清理它们。

2) 我的日期应该采用什么格式?我尝试了 '2018-01-01 00:00:00.000' 和 '2018-01-01' 和两个错误。

代码:

sql =count_query(@start_date_time, sender_type, sender_id)
records_array = ActiveRecord::Base.connection.execute(sql)

错误

ActiveRecord::StatementInvalid (PG::UndefinedFunction: 
ERROR:  operator does not exist: timestamp without time zone > integer)

LINE 11: WHERE created_at > 2018-01-01

【问题讨论】:

  • 我对rails的了解还不够,无法告诉你如何解决这个问题,但是错误是由于缺少引用引起的。它正在尝试将 2016 (2018 - 1 - 1) 与 created_at 进行比较。

标签: postgresql activerecord ruby-on-rails-5.2


【解决方案1】:

WHERE created_at > '2018-01-01' 应该可以工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 2021-10-03
    • 2010-09-22
    相关资源
    最近更新 更多