Clojure 使用Korma和MySQL强制转换空日期字段
我试图使用Korma和MySQL从一个名为posts的表中进行选择,默认情况下,发布日期的字段为零Clojure 使用Korma和MySQL强制转换空日期字段,clojure,korma,Clojure,Korma,我试图使用Korma和MySQL从一个名为posts的表中进行选择,默认情况下,发布日期的字段为零 mysql> describe posts; +-----------+--------------+------+-----+---------------------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+--
mysql> describe posts;
+-----------+--------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------------------+-------+
| id | int(11) | NO | PRI | 0 | |
| title | varchar(250) | YES | | NULL | |
| content | text | YES | | NULL | |
| status | tinyint(1) | YES | | 0 | |
| created | timestamp | NO | | CURRENT_TIMESTAMP | |
| published | timestamp | NO | | 0000-00-00 00:00:00 | |
| author | int(11) | NO | MUL | NULL | |
+-----------+--------------+------+-----+---------------------+-------+
7 rows in set (0.02 sec)
如果我尝试选择并使用“已发布”字段,则会出现以下错误:
user=>selectposts字段:id:title:content:status:created
:已发布无法使用SQL执行查询:SELECT posts.id,
posts.title、posts.content、posts.status、posts.created、,
posts.published FROM posts::[]ClassCastException
无法将java.lang.RuntimeException转换为java.sql.SQLException
clojure.java.jdbc/print-sql-exception jdbc.clj:350
如果不使用“已发布”字段,则一切正常:
user=>selectposts字段:id:title:content:status:created
:author[{:id 1,:title Hello World!,:内容欢迎访问
Beats,世界上最先进的Clojure Beat引擎!,:状态
true,:created,:author 1}{:id 2,
:标题“你好,世界!”,再次!,:内容为“t”增益!欢迎
为了Beats,世界上最先进的Clojure Beat引擎!,
:状态为真,:已创建,:作者2}]
我如何处理这个领域?我试图用一个简单的log语句向我的实体添加一个转换函数,但它似乎没有被调用。假设Marc B是正确的,并且问题的原因是您有一个发布为0000-00-00的记录,那么您可以将zeroDateTimeBehavior=convertToNull添加到Korma mysql:db密钥中,例如:
:db "mydb?zeroDateTimeBehavior=convertToNull"
现在JDBC和Korma将为该记录返回已发布的nil:
(select posts (fields :id :published))
;; [{:published nil, :id 1}]
您可能必须重新启动repl,标志才能生效
另见:
实际上,published在NULL列中定义为notnull NO。如果不提供值,mysql将填充默认的0000-00-00时间戳,实际上Marc是正确的。您还应该尝试解析完整的堆栈跟踪:使用'clojure.stacktrace打印堆栈跟踪*e