-
Notifications
You must be signed in to change notification settings - Fork 808
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
Can't encode float64 into oid 1700 #112
Comments
Delving a bit deep in the code it seems the issue is that numeric() is not supported.. I switched out numeric for float in the sql file and then it proceeded to work. Interestingly enough, when using sqlx with lib/pq this was not an issue. |
The reason pgx does not natively decode and encode numeric is Go does not have a standard decimal type. Any mapping to or from a Go float is potentially losing data. The encoding/decoding logic is pretty trivial, probably less than 20 lines of code -- the problem is what Go type to map PostgreSQL numeric to. |
For what it's worth, in 9f9a977 I added compatibility with the standard database/sql.Scanner and database/sql/driver.Valuer interfaces for custom types. This means you can use https://github.com/shopspring/decimal as a type that maps to PostgreSQL numeric. See https://github.com/jackc/pgx/blob/master/query_test.go#L913 for an example. |
Nice.. Thanks |
What about math/big.Rat? It can represent any numeric type without losing precision. |
That's true. But https://github.com/shopspring/decimal gives some reason why you might not want to use math/bin.Rat for money (the traditional use case for decimal / numeric). Though, I suppose it still could be reasonable to have math/bin.Rat as the default mapping. |
@jackc is there a way to override this behavior? I'm already mapping floats to numeric using |
The alternative is to convert to and from a new struct, copying every field except converting float to decimal. It works, but it sucks. |
Hi, I'm using the library with sqlx and am getting this error for a struct like the following:
and keep getting a :
Cannot encode float64 into oid 1700 - float64 must implement Encoder or be converted to a string
In my postgres database:
Latitude is numeric(8,6),
Longitude numeric(9,6),
If i change the latitude or longitude to be pgx.NullFloat64 then I get
json: cannot unmarshal number into Go value of type pgx.NullFloat64
The text was updated successfully, but these errors were encountered: