Skip to content

Error inserting zero time in TIME data type column #1091

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
xhit opened this issue May 2, 2020 · 1 comment
Closed

Error inserting zero time in TIME data type column #1091

xhit opened this issue May 2, 2020 · 1 comment

Comments

@xhit
Copy link

xhit commented May 2, 2020

Issue description

The issue #725 was solved appending 0000-00-00 to b

mysql/packets.go

Line 1117 in 681ffa8

b = append(b, "0000-00-00"...)

But doesn't work if the column to insert is TIME. Error same with #725
Incorrect datetime value: '0000-00-00' for column...

Skipping the append is not a solution because a zero time will be added like with date 0001-01-01 for data types that use a date and it's a work around is to insert 0000-00-00

Example code

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 TIME(6))")
	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)
	}
}

Configuration

Driver version (or git SHA): v1.5.0

Go version: 1.13.9

Server version: MySQL 5.6

Server OS: Windows 10

@methane
Copy link
Member

methane commented May 3, 2020

This driver assumes time.Time represents unixtime, or date with time.
The driver can not distinguish you are using time.Time for time without date. So it can not be supported.
You should format the time value by yourself.

@methane methane closed this as completed May 3, 2020
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

2 participants