Skip to content
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

Closed
isadon opened this issue Dec 15, 2015 · 8 comments
Closed

Can't encode float64 into oid 1700 #112

isadon opened this issue Dec 15, 2015 · 8 comments

Comments

@isadon
Copy link

isadon commented Dec 15, 2015

Hi, I'm using the library with sqlx and am getting this error for a struct like the following:

type PostPerson struct {
    Username    *string     `json:"username" binding:"exists"`
    Photo1URL  pgx.NullString `json:"photo1Url"`
    Latitude    *float64    `json:"latitude" binding:"exists"`
    Longitude   *float64    `json:"longitude" binding:"exists"`
}

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

@isadon
Copy link
Author

isadon commented Dec 15, 2015

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.

@isadon isadon closed this as completed Dec 15, 2015
@isadon isadon reopened this Dec 15, 2015
@jackc
Copy link
Owner

jackc commented Dec 15, 2015

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.

@jackc
Copy link
Owner

jackc commented Dec 31, 2015

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.

@isadon
Copy link
Author

isadon commented Dec 31, 2015

Nice.. Thanks

@jackc jackc closed this as completed Mar 7, 2016
@reusee
Copy link

reusee commented May 25, 2016

What about math/big.Rat? It can represent any numeric type without losing precision.

@jackc
Copy link
Owner

jackc commented May 26, 2016

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.

@aj0strow
Copy link

@jackc is there a way to override this behavior? I'm already mapping floats to numeric using gocraft/dbr and want to gradually transition to pgx.

@aj0strow
Copy link

The alternative is to convert to and from a new struct, copying every field except converting float to decimal. It works, but it sucks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants