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