You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I am kind of new to pgx and I am trying to understand how to use pgtype.
I am also using sqlc, but that should make no difference.
I am writing a test that writes a struct to a postgres db, then the db is supposed to return back the value.
I am finding the use quite verbose, but I might be missing some thing.
I need to convert Go types into pgtype types (i.e. string to pgtype.Text{String: city, Valid: true}) to write to the db.
Than, the value that I receive from the read is not a string and I need to convert it again.
// This is my CreateWeather function signature where I call// [QueryRow](https://pkg.go.dev/github.com/jackc/pgx/[email protected]#Conn.QueryRow)func (q*Queries) CreateWeather(ctx context.Context, argCreateWeatherParams) (Weather, error) {
row:=q.db.QueryRow(ctx, createWeather,
arg.Temperature,
arg.City,
arg.LocalTime,
arg.Updated,
)
variWeathererr:=row.Scan(
&i.ID,
&i.Temperature,
&i.City,
&i.LocalTime,
&i.Updated,
)
returni, err
}
// Invoking the above function in my testtemp:=12city:="London"weatherResponse, err:=q.CreateWeather(ctx, CreateWeatherParams{
Temperature: pgtype.Numeric{Int: big.NewInt(int64(temp)), Exp: 0, Valid: true},
City: pgtype.Text{String: city, Valid: true},
LocalTime: pgtype.Timestamp{Time: time.Now(), Valid: true},
Updated: pgtype.Date{Time: time.Now(), Valid: true},
})
// Then my tests:require.Equal(t, temp, weatherResponse.Temperature)
require.Equal(t, city, weatherResponse.City)
Then my tests fail with:
expected: int(12)
actual : pgtype.Numeric(pgtype.Numeric{Int:1200, Exp:-2, NaN:false, InfinityModifier:0, Valid:true})
expected: string("London")
actual : pgtype.Text(pgtype.Text{String:"London", Valid:true})
Then, if I do fmt.Printf("Just City %+v\n", weatherResponse.City) I get pgtype.Text{String:"London", Valid:true}.
So, this means I have to do correctCityName := weatherResponse.City.String?
Then, if I use temp := 12 I get weatherResponse.Temperature = {Int:+1200 Exp:-2 NaN:false InfinityModifier:finite Valid:true}.
The only way I made it work was by using using Temperature pgtype.Int4, then in my test I would call CreateWeather with Temperature: pgtype.Int4{Int32: int32(temp), Valid: true},, then assert with require.Equal(t, temp, int(weatherResponse.Temperature.Int32)).
So, is it normal to have go struct => pgtype => db, then db => pgtype => go struct?
I am really confused on how to use correctly the library, any help is appreciated.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello, I am kind of new to
pgx
and I am trying to understand how to usepgtype
.I am also using sqlc, but that should make no difference.
I am writing a test that writes a struct to a postgres db, then the db is supposed to return back the value.
I am finding the use quite verbose, but I might be missing some thing.
I need to convert Go types into
pgtype
types (i.e.string
topgtype.Text{String: city, Valid: true}
) to write to the db.Than, the value that I receive from the read is not a string and I need to convert it again.
Then my tests fail with:
Then, if I do
fmt.Printf("Just City %+v\n", weatherResponse.City)
I getpgtype.Text{String:"London", Valid:true}
.So, this means I have to do
correctCityName := weatherResponse.City.String
?Then, if I use
temp := 12
I getweatherResponse.Temperature = {Int:+1200 Exp:-2 NaN:false InfinityModifier:finite Valid:true}
.The only way I made it work was by using using
Temperature pgtype.Int4
, then in my test I would callCreateWeather
withTemperature: pgtype.Int4{Int32: int32(temp), Valid: true},
, then assert withrequire.Equal(t, temp, int(weatherResponse.Temperature.Int32))
.So, is it normal to have go struct => pgtype => db, then db => pgtype => go struct?
I am really confused on how to use correctly the library, any help is appreciated.
Thanks,
Beta Was this translation helpful? Give feedback.
All reactions