sql - HugSQL错误: column "date_answer" is of type date but expression is of type character varying?

标签 sql date parsing clojure hugsql

我正在使用 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

最佳答案

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

(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)]]

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

关于sql - HugSQL错误: column "date_answer" is of type date but expression is of type character varying?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61772085/

相关文章:

sql - 使用eclipse数据工具多条sql语句(全部执行)

php - 存储和显示最近访问过他人页面的人员的最有效的数据库方法是什么?

Javascript - 解析html字符串获取具有类X的每个元素并返回它们的数组

sql - 如何在 SQL WHERE 条件上创建 IF 而无需 ELSE?

sql - 如何将 MS SQL Server 一个数据库中所有表的所有列名更改为大写?

javascript - javascript 中的日期格式格式错误

python - Pyparsing:如何实现C风格注释的特殊处理?

python - 将二元运算符添加到 z3

java - Android 中 localTime 和 localDate 的替代类是什么?

javascript - 在 Mysql 和 Javascript 中获取同一日期的不同时间戳?