【问题标题】:HugSQL Error: column "date_answer" is of type date but expression is of type character varying?HugSQL 错误:列“date_answer”是日期类型,但表达式是字符类型不同?
【发布时间】:2020-05-13 19:23:20
【问题描述】:

我正在使用clojure hugSQL 将数据插入PostgreSQL 数据库。 我正在尝试使用 :tuple* 参数将多行插入答案表。传递日期时出现以下错误:

Error: column "date_answer" is of type date but expression is of type character varying?

HugSQL 创建的示例 SQL 查询:

INSERT INTO answer (a, b, c, d, date_answer) VALUES (62,76,NULL,NULL,'2020-05-13')

使用终端插入时,相同的查询可以正常工作,因此字符串的格式似乎没问题。 有什么方法可以在 :tuple* 参数中指定单个字段,以便我可以执行类似 :date_answer::date

的操作

这是我的 HugSQL 查询:

INSERT INTO answer (a, b, c, d, date_answer) VALUES :tuple*:answers

【问题讨论】:

    标签: sql date parsing clojure hugsql


    【解决方案1】:

    您必须解析传递给日期的值。例如,

    (ns your-ns
      ...
      (:import
       (java.time LocalDate)
       (java.time.format DateTimeFormatter DateTimeParseException)))
    
    (def ^:private yyyy-MM-dd-formatter
      (DateTimeFormatter/ofPattern "yyyy-MM-dd"))
    
    ;; Assuming your HugSQL function is called insert
    (insert {:answers [[a b c d (LocalDate/parse date-answer yyyy-MM-dd-formatter)]]
    

    那么你的日期答案是“日期”类型,而不是“字符变化”。

    【讨论】:

      猜你喜欢
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      • 2018-10-09
      • 2021-06-18
      • 2015-04-22
      • 2011-12-18
      • 1970-01-01
      相关资源
      最近更新 更多