如何在mysql表中保存time.Now(),列名为created_at timestamp null。
我收到错误消息:
Error:Error 1292: Incorrect datetime value: '2017-08-05 19:06:14.190 +0000' for column 'created_at' at row 1更多信息被问到:- (我正在使用fragmenta cms,所以所有的参考代码和它们的行号都在下面)
表架构:-
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 | |
+----------------------+--------------+------+-----+---------+----------------+运行它以保存的代码:-
在这里的第62行( https://github.com/fragmenta/fragmenta-cms/blob/master/src/pages/actions/setup.go#L62 )
它调用代码user := users.New()
在这里的文件中的第51行( https://github.com/fragmenta/fragmenta-cms/blob/master/src/users/query.go#L51 )
设置了New()函数。
这就像:-
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
}他们的连接/ mysql打开模式位于这里( https://github.com/fragmenta/query/blob/master/adapters/database_mysql.go#L23 )。
发布于 2017-08-06 08:41:25
https://github.com/fragmenta/query中有一个错误。query/adapters/database.go中的TimeString方法并非对所有DBMS适配器都有效。
// 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")
}对于MySQL时间戳:MySQL 5.7 Reference Manual, 11.3.1 The DATE, DATETIME, and TIMESTAMP Types无效。query/adapters/database_mysql.go中的MySQL TimeString方法应该是:
// 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")
}发布于 2017-08-06 04:42:40
您正尝试使用字符串query.go:36插入它
now := query.TimeString(time.Now().UTC())它是由您使用的database.go:59包生成的
return t.Format("2006-01-02 15:04:05.000 -0700")MySQL希望它采用yyyy-MM-dd hh:mm:ss模式,请使用以下代码片段将该模式应用于当前的Time.time对象:
now := time.Now().UTC().Format("2006-01-02 03:04:05")不管怎样,为什么不在插入记录时使用SQL函数NOW()呢?
https://stackoverflow.com/questions/45525539
复制相似问题