Mysql: Error: "Incorrect datetime value: '0000-00-00' for column..." on mysql 5.7 and above

Created on 22 Dec 2017  路  6Comments  路  Source: go-sql-driver/mysql

Issue description

I've inserted a "zero" time.Time object to a table with a DATETIME column, and get Incorrect datetime value: '0000-00-00' for column error.

Example code

The below code fails:

package main

import (
    "database/sql"
    "log"
    "time"

    _ "github.com/go-sql-driver/mysql"
)

func main() {
    db, _ := sql.Open("mysql", "root:secret@(127.0.0.1:3306)/test")
    _, err := db.Exec("CREATE TABLE IF NOT EXISTS daty (col DATETIME)")
    if err != nil {
        log.Panicf("Failed create: %s", err)
    }
    _, err = db.Exec("INSERT INTO daty (col) VALUES (?)", time.Time{})
    if err != nil {
        log.Panicf("Failed insert: %s", err)
    }
}

Replacing time.Time{} with time.Now() will cause the code not to fail.

Configuration

*Driver version (or git SHA): 9181e3a86a19bacd63e68d43ae8b7b36320d8092 (master)
*Go version: go version go1.9 linux/amd64
*Server version: mysql 8.0.3, 5.7.20
*Server OS: fedora 26

All 6 comments

It's MySQL's spec and there are nothing we can.
You should use only valid datetime with MySQL.

It's MySQL's spec and there are nothing we can.
You should use only valid datetime with MySQL.

Hi, thanks!
Using TIMESTAMP instead would work?

This is not MySQL user forum...

I know much about MySQL protocol, but I don't know much about MySQL's behavior.

Was this page helpful?
0 / 5 - 0 ratings