douou8954 2017-08-05 19:13
浏览 79
已采纳

mysql timestamp error with time.Now()Golang

How to save time.Now() in mysql table, column name as created_at timestamp null.

I am getting error :

Error:Error 1292: Incorrect datetime value: '2017-08-05 19:06:14.190 +0000' for column 'created_at' at row 1

More Information as asked :- ( I am using fragmenta cms, so all reference code with their line number is given below )

Table schema :-

mysql> describe users;
+----------------------+--------------+------+-----+---------+----------------+
| Field                | Type         | Null | Key | Default | Extra          |
+----------------------+--------------+------+-----+---------+----------------+
| id                   | int(11)      | NO   | PRI | NULL    | auto_increment |
| created_at           | timestamp    | YES  |     | NULL    |                |
| updated_at           | timestamp    | YES  |     | NULL    |                |
| status               | int(11)      | YES  |     | NULL    |                |
| role                 | int(11)      | YES  |     | NULL    |                |
| name                 | varchar(250) | YES  |     | NULL    |                |
| email                | varchar(250) | YES  |     | NULL    |                |
| title                | varchar(250) | YES  |     | NULL    |                |
| summary              | text         | YES  |     | NULL    |                |
| text                 | text         | YES  |     | NULL    |                |
| image_id             | int(11)      | YES  |     | NULL    |                |
| password_hash        | varchar(250) | YES  |     | NULL    |                |
| password_reset_token | text         | YES  |     | NULL    |                |
| password_reset_at    | timestamp    | YES  |     | NULL    |                |
+----------------------+--------------+------+-----+---------+----------------+

Code that is running it to save :-

At line no. 62 here ( https://github.com/fragmenta/fragmenta-cms/blob/master/src/pages/actions/setup.go#L62 )

It calls code user := users.New()

in Line no. 51 at file here ( https://github.com/fragmenta/fragmenta-cms/blob/master/src/users/query.go#L51 )

New() function is setup.

Which is like :-

func New() *User {
    user := &User{}
    user.CreatedAt = time.Now()
    user.UpdatedAt = time.Now()
    user.TableName = TableName
    user.KeyName = KeyName
    user.Status = status.Draft
    return user
}

and their connecting / mysql opening pattern is located here ( https://github.com/fragmenta/query/blob/master/adapters/database_mysql.go#L23 ) .

  • 写回答

2条回答 默认 最新

  • douquanhui5735 2017-08-06 00:41
    关注

    There is a bug in https://github.com/fragmenta/query. The TimeString method in query/adapters/database.go is not valid for all DBMS adapters.

    // TimeString - given a time, return the standard string representation
    func (db *Adapter) TimeString(t time.Time) string {
        return t.Format("2006-01-02 15:04:05.000 -0700")
    }
    

    It's not valid for a MySQL timestamp: MySQL 5.7 Reference Manual, 11.3.1 The DATE, DATETIME, and TIMESTAMP Types. The MySQL TimeString method in query/adapters/database_mysql.go should be:

    // TimeString - given a time, return the MySQL standard string representation
    func (db *MysqlAdapter) TimeString(t time.Time) string {
        return t.Format("2006-01-02 15:04:05.999999")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C