-
Notifications
You must be signed in to change notification settings - Fork 835
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
Valuer interface is not respected for nil values #1860
Labels
Comments
Duplicate of #1566. This was changed in v5. There's a few possible changes that would bring back that behavior from v4, but progress seems stalled at the moment. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe the bug
When using a struct that implements
driver.Valuer
but is backed by a nilable type,Value
is never called when the value isnil
. This is used by some types where a default should be stored within the DB. The default converter fordatabase/sql
will call this on a nil.I'm using the stdlib compatibility layer. When tracing through the code, the
database/sql
code will call the CheckNamedValue method on*stdlib.Conn
, which will then just pass through the values directly.Later on, when the prepared statement is being built,
anynil.NormalizeSlice
is called on the arguments, which then sets the value of the arg as nil and erases any type information about the arg.The nil value hits a nil check, preventing any Encode plan from being executed to convert the value / call
Value
on it.To Reproduce
See code below. The first insert will result in a
NULL
value to be inserted, while the second one will insert an empty object of{}
.Please run your example with the race detector enabled. For example,
go run -race main.go
orgo test -race
.Expected behavior
The
Value
method is called on a nil type that implementsdriver.Valuer
to allow for any default valueActual behavior
The
Value
method is not called / the value is not convertedVersion
$ go version
-> go1.20.4 darwin/arm64$ psql --no-psqlrc --tuples-only -c 'select version()'
-> [e.g. PostgreSQL 14.4 on x86_64-apple-darwin21.5.0, compiled by Apple clang version 13.1.6 (clang-1316.0.21.2.5), 64-bit]$ grep 'github.com/jackc/pgx/v[0-9]' go.mod
-> v5.5.1Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: