Working with pgtypes #2910
Replies: 4 comments 2 replies
-
Same issue with pgtype.Numeric generated by SQLC. I found the structure difficult to manipulate when trying to insert a float. The only way I found is the way you mention, as changing type in DDL (like going from Numeric type to Float8) does not change the generated type (pgtype.Numeric): pgtype.Numeric{
Int: big.NewInt(453),
Exp: -2,
Status: pgtype.Present,
} |
Beta Was this translation helpful? Give feedback.
-
Same thing here, is there anyone that can point us in the right direction? |
Beta Was this translation helpful? Give feedback.
-
Here's a working solution for creating package main
import (
"fmt"
"github.com/jackc/pgx/v5/pgtype"
)
func main() {
f := 5.527
num := &pgtype.Numeric{}
err := num.Scan(fmt.Sprintf("%.2f", f)) // .2 specifies precision
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", num)
} Result:
Inspired by this answer on StackOverflow. |
Beta Was this translation helpful? Give feedback.
-
How to convert pgtype.Numeric to base 10 string? pgtype v4 has a method AssignTo can do this, but this method is missing in v5. |
Beta Was this translation helpful? Give feedback.
-
When using
pgx/v5
the INSERT clause for the following schema:Will generate this output:
I am having a very hard time trying to use the
CreateItem()
method usingpgtypes
. I have found online that fortext
columns I can do:However, for pgtype.Numeric it is more complicated since, for example, to represent 4.53 it seems like I have to do:
I do not want to use overrides in the
sqlc
configuration, sincepgtypes
are intented to offer better results than Go's built-in types. However usingpgtype
'as-is' I think it would require to create some methods to convert Go types topgtypes
.My question is: Am I missing something here? Is this the way it is supposed to work? How do you usually work with these types? Any real-use example would be ideal.
Beta Was this translation helpful? Give feedback.
All reactions