lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
8.86k stars 908 forks source link

Array types can't convert {NULL} #1131

Closed AuroraTea closed 12 months ago

AuroraTea commented 1 year ago

pq.Int64Array and other Array types, in {NULL} will report an error pq: parsing array element index 0: strconv.ParseInt: parsing "": invalid syntax And {NULL} is still very common in LEFT OUTER JOIN

cbandy commented 1 year ago

Yep. For nullable values use []sql.NullInt64 and pq.Array(). From the docs:

var x []sql.NullInt64
db.QueryRow(`SELECT ARRAY[235, 401]`).Scan(pq.Array(&x))
AuroraTea commented 1 year ago

New to the use of pq.Array(&x), thank you @cbandy very much!

I usually prefer to use int64 (NULL will be nil), but[]int64 here is not work withparsing array element index 0: strconv.ParseInt: parsing "": invalid syntax[], []sql.NullInt64 is OK. Is there still a way to use *int64?