Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
posener opened this issue Dec 22, 2017 · 6 comments

Comments

@posener
Copy link

posener commented Dec 22, 2017

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

@methane
Copy link
Member

methane commented Dec 22, 2017

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

1 similar comment
@methane
Copy link
Member

methane commented Dec 22, 2017

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

@methane methane closed this as completed Dec 22, 2017
@posener
Copy link
Author

posener commented Dec 22, 2017

Hi, thanks!
Using TIMESTAMP instead would work?

@methane
Copy link
Member

methane commented Dec 22, 2017

This is not MySQL user forum...

@methane
Copy link
Member

methane commented Dec 22, 2017

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

@ianaz
Copy link

ianaz commented Jan 22, 2019

Related issue: #346

Amygos added a commit to nethesis/icaro that referenced this issue Aug 6, 2019
From Tuesday April 23rd, 2019, Travis-ci started to use Ubuntu Xenial as
default distro[1], this introduce new mysql version 5.7.x[2] and incompatible
datetime format[3].

[1] https://blog.travis-ci.com/2019-04-15-xenial-default-build-environment
[2] https://docs.travis-ci.com/user/database-setup/#mysql
[3] go-sql-driver/mysql#725
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants