Describe the bug
I was trying to use the bulk insertion feature mentioned by docs. The bug arises when one needs to insert a time.Time value.
The BulkInserter in go-zero is using a function writeValue, in which Time.String() is being used to convert a time.Time variable into string. Time.String() returns a format like "2006-01-02 15:04:05.999999999 -0700 MST" that is not accepted by MySQL, which i believe is the cause of this bug.
func writeValue(buf *strings.Builder, arg any) {
// ...
case time.Time:
buf.WriteByte('\'')
buf.WriteString(v.String())
buf.WriteByte('\'')
To Reproduce
I wrote a unittest case to reproduce this problem
CREATE TABLE
IF NOT EXISTS greet (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(64) NOT NULL,
`released_at` DATETIME NOT NULL,
PRIMARY KEY(`id`)
) ENGINE=InnoDB;
func TestBulkInsert(t *testing.T) {
conn := sqlx.NewMysql("root:MySQL@2026@tcp(127.0.0.1:3306)/gzgreet?parseTime=true&loc=Asia%2FShanghai&charset=utf8mb4,utf8")
inserter, _ := sqlx.NewBulkInserter(
conn,
fmt.Sprintf("INSERT INTO `greet` (%s) VALUES (?, ?)", greetRowsExpectAutoSet),
)
defer inserter.Flush()
require.NoError(t, inserter.Insert("bob", time.Now()))
done := make(chan error)
inserter.SetResultHandler(func(r sql.Result, err error) {
done <- err
})
result := <- done
assert.NoError(t, result)
}
The error is
Incorrect datetime value: '2026-03-28 17:26:54.88284 +0800 CST m=+0.007881418' for column 'released_at' at row 1
The SQL generated by bulk inserter
INSERT INTO `greet` (`name`,`released_at`) VALUES ('bob', '2026-03-28 17:26:54.88284 +0800 CST m=+0.007881418')
{"@timestamp":"2026-03-28T17:26:55.999+08:00","caller":"sqlx/stmt.go:274","content":"sql exec: INSERT INTO `greet` (`name`,`released_at`) VALUES ('bob', '2026-03-28 17:26:54.88284 +0800 CST m=+0.007881418')","duration":"24.3ms","level":"info"}
{"@timestamp":"2026-03-28T17:26:55.999+08:00","caller":"sqlx/utils.go:147","content":"stmt: INSERT INTO `greet` (`name`,`released_at`) VALUES ('bob', '2026-03-28 17:26:54.88284 +0800 CST m=+0.007881418'), error: Error 1292 (22007): Incorrect datetime value: '2026-03-28 17:26:54.88284 +0800 CST m=+0.007881418' for column 'released_at' at row 1","level":"error"}
Expected behavior
Rows are inserted without error
Screenshots
If applicable, add screenshots to help explain your problem.
Environments (please complete the following information):
- OS: macOS
- go-zero version v1.10.0
- goctl version 1.9.2 darwin/arm64
- MySQL 8.0.43
More description
Add any other context about the problem here.
Describe the bug
I was trying to use the bulk insertion feature mentioned by docs. The bug arises when one needs to insert a
time.Timevalue.The BulkInserter in go-zero is using a function
writeValue, in whichTime.String()is being used to convert atime.Timevariable into string.Time.String()returns a format like"2006-01-02 15:04:05.999999999 -0700 MST"that is not accepted by MySQL, which i believe is the cause of this bug.To Reproduce
I wrote a unittest case to reproduce this problem
CREATE TABLE IF NOT EXISTS greet ( `id` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(64) NOT NULL, `released_at` DATETIME NOT NULL, PRIMARY KEY(`id`) ) ENGINE=InnoDB;The error is
The SQL generated by bulk inserter
Expected behavior
Rows are inserted without error
Screenshots
If applicable, add screenshots to help explain your problem.
Environments (please complete the following information):
More description
Add any other context about the problem here.