Skip to content

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

Closed
@posener

Description

@posener

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): 9181e3a (master)
*Go version: go version go1.9 linux/amd64
*Server version: mysql 8.0.3, 5.7.20
*Server OS: fedora 26

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions