Skip to content

Problem with database/sql Nullable struct #706

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
linxGnu opened this issue Nov 16, 2017 · 5 comments
Closed

Problem with database/sql Nullable struct #706

linxGnu opened this issue Nov 16, 2017 · 5 comments
Labels
Milestone

Comments

@linxGnu
Copy link
Contributor

linxGnu commented Nov 16, 2017

Issue description

Problem with insert/update null field using sql.NullInt64 and others database/sql Nullable struct

Example code

package main

import (
	_ "github.com/go-sql-driver/mysql"

	"database/sql"
	"fmt"
)

func main() {
	dsn := "root:123@tcp(localhost:3306)/tmp?charset=utf8&collation=utf8_general_ci&parseTime=true&loc=Asia%2FHo_Chi_Minh"
	db, _ := sql.Open("mysql", dsn)

	fmt.Println(db.Exec("INSERT INTO test (id, phone, address) VALUES (?, ?, ?)", 10, sql.NullInt64{Valid: true, Int64: 123}, sql.NullString{Valid: true, String: "abc"}))
}

Error log

<nil> sql: converting argument $2 type: unsupported type sql.NullInt64, a struct

Configuration

Driver version (or git SHA): v1.3-46-g78d399c

Go version: go1.9.2 darwin/amd64

Server version: MariaDB 10.2

Server OS: MacOS X 10.13.1

@percona-csalguero
Copy link

Is it correct that you have 3 fields in your fields list but only 2 placeholders and values?

@julienschmidt
Copy link
Member

sql.NullInt64 is a Scan struct, not for inserting. That means it should be used for retrieving values from the DB, not the other way round.
If you want to insert the value of such struct, then access it via .Value

@linxGnu
Copy link
Contributor Author

linxGnu commented Nov 16, 2017

@julienschmidt : sql.NullInt64 also has driver Valuer interface which is for inserting/updating to database. I did revert to previous version of the library (v1.3-45) and things are fine.

@percona-csalguero : thanks for your quick look, I have updated the code above.

@julienschmidt
Copy link
Member

Is then maybe #708 the reason?

@linxGnu
Copy link
Contributor Author

linxGnu commented Nov 16, 2017

I think the main reason is similar to #708.

But returning driver.ErrSkip as in #708 is not true. The lib should call Valuer interface func when parsing instead of returning error. Only error returning from interface should be caught.

@julienschmidt julienschmidt added this to the v1.4 milestone Nov 17, 2017
bLamarche413 pushed a commit to bLamarche413/mysql that referenced this issue Mar 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants