-
Notifications
You must be signed in to change notification settings - Fork 28
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
Encoding of Numeric inside a custom type panics #39
Comments
Thanks for the detailed report. I can reproduce. The error is that pgtype.Numeric cannot be
That's the Go representation of a Numeric type, 5044e-2 (assuming you meant 50.44 instead of 55.44). Here's the minimal reproducible case: func TestNewQuerier_SetNumericType(t *testing.T) {
num := &pgtype.Numeric{}
require.NoError(t, num.Set(64))
decoder := &pgtype.Numeric{}
err := decoder.Set(num)
require.NoError(t, err) // same error here
} I'm not sure if this is intended behavior by pgtype. I expected that pgtype.Numeric could be As a workaround, I tried to set |
So, we're a bit stuck. Here's the options:
Short term, you're best bet is probably to rely on an external type and register the encoder and decoder: https://github.com/jackc/pgx/wiki/Numeric-and-decimal-support. Medium term, |
I added an example of how to make this work with a real Decimal type in 7f87888. The main trick is to register the data type on the Querier: q := NewQuerierConfig(conn, QuerierConfig{
DataTypes: []pgtype.DataType{
{
Value: &shopspring.Numeric{},
Name: "numeric",
OID: pgtype.NumericOID,
},
},
}) And then, when invoking pggen, use: The pgx recommendation below won't work because there's not a way to get registered DataTypes of all types of pgx connections, including pgx.Conn, and pgxpool.Pool. The pgx recommendation for pgxpool is to use an AfterConnect hook to call RegisterDataType but pggen can't "see" that occur. // pgx way that won't work with pggen.
conn.ConnInfo().RegisterDataType(pgtype.DataType{
Value: &shopspring.Numeric{},
Name: "numeric",
OID: pgtype.NumericOID,
}) |
@jschaf Thank you for your quick and detailed feedback. I was already using float64 as input, so the workaround with |
I receive a panic when I try to insert entries with custom types containing Numeric fields. The relevant schema looks like this:
I get the following error message If I try to insert a row with
value = 55.44
:I guess he is trying to convert a Numeric struct to a Numeric struct, but I might be wrong. This is the generated code that causes the panic:
Considering that the comment says
should always succeed
I'm inclined to think that this might be a bug.The text was updated successfully, but these errors were encountered: