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
Describe the bug
When the name of a database column includes and underscore character, pgx.CollectRows with pgx.RowToStructByName fails to to correctly marshall the data into a struct.
To Reproduce
The following code will produce the bug. The values stored in column_one and column_three will fail to be carried over into the struct, but the values stored in columntwo and columnfour will correctly carry over.
This example is also available in the github repository https://github.com/Aerek-Yasa/pgxtest .
The test requires the test runner to fill in the credentials and location for a test database at the top of the file. The test database is expected to already be created.
package main
import (
"context""fmt""github.com/jackc/pgx/v5""github.com/jackc/pgx/v5/pgxpool"
)
vardatabaseUserstring=""vardatabasePasswordstring=""vardatabaseHoststring=""vardatabasePortint32=0vardatabaseNamestring=""typePGXTestTablestruct {
ColumnOneint32`db:"column_one"`ColumnTwoint32`db:"columntwo"`ColumnThree []int32`db:"column_three"`ColumnFour []int32`db:"columnfour"`
}
typeWrapperstruct {
Values []PGXTestTable`db:"values"`
}
funcmain() {
ifdatabaseUser=="" {
fmt.Println("Please add the username for the test database into the main.go file")
return
}
ifdatabasePassword=="" {
fmt.Println("Please add the password for the test database into the main.go file")
return
}
ifdatabaseHost=="" {
fmt.Println("Please add the hostname for the test database into the main.go file")
return
}
ifdatabasePort==0 {
fmt.Println("Please add the port for the test database into the main.go file")
return
}
ifdatabaseName=="" {
fmt.Println("Please add the database name for the test database into the main.go file")
return
}
configString:=fmt.Sprintf("user=%s password=%s host=%s port=%d dbname=%s", databaseUser, databasePassword, databaseHost, databasePort, databaseName)
config, err:=pgxpool.ParseConfig(configString)
iferr!=nil {
fmt.Println(err)
return
}
pool, err:=pgxpool.NewWithConfig(context.Background(), config)
iferr!=nil {
fmt.Println(err)
return
}
_, err=pool.Exec(context.Background(), ` CREATE TABLE IF NOT EXISTS pgx_test_table ( column_one INT, columntwo INT, column_three INT[], columnfour INT[] ); `)
iferr!=nil {
fmt.Println(err)
return
}
_, err=pool.Exec(context.Background(), ` INSERT INTO pgx_test_table (column_one, columntwo, column_three, columnfour) VALUES (1001, 1011, '{1110}', '{1100}'), (2002, 2022, '{2220}', '{2200}'); `)
iferr!=nil {
fmt.Println(err)
return
}
rows, err:=pool.Query(context.Background(), ` SELECT COALESCE( (SELECT json_agg(row_to_json(pgx_test_table)) FROM pgx_test_table ), '[]'::json ) values `)
iferr!=nil {
fmt.Println(err)
return
}
deferrows.Close()
wrapper, err:=pgx.CollectRows[Wrapper](rows, pgx.RowToStructByName[Wrapper])
iferr!=nil {
fmt.Println(err)
return
}
for_, thing:=rangewrapper[0].Values {
fmt.Println("Got values: ", thing.ColumnOne, thing.ColumnTwo, thing.ColumnThree, thing.ColumnFour)
}
}
Expected behavior
The program is expected to output the values :
1001, 1011, [1110] and [1100] for the first database row
2002, 2022, [2220] and [2200] for the second database row
Actual behavior
The program outputs the following values;
0, 1011, [ ] and [1100] for the first database row
0, 2022, [ ], and [2200] for the second database row
Version
Go: go version go1.22.4 windows/amd64
PostgreSQL: PostgreSQL 15.2, compiled by Visual C++ build 1914, 64-bit'`
pgx: v5.6.0 and v5.7.2 tested
The text was updated successfully, but these errors were encountered:
pgx doesn't do any json parsing on its own. It uses the encoding/json package. If some of your fields aren't being populated I suspect you need json tags.
Describe the bug
When the name of a database column includes and underscore character,
pgx.CollectRows
withpgx.RowToStructByName
fails to to correctly marshall the data into a struct.To Reproduce
The following code will produce the bug. The values stored in
column_one
andcolumn_three
will fail to be carried over into the struct, but the values stored incolumntwo
andcolumnfour
will correctly carry over.This example is also available in the github repository https://github.com/Aerek-Yasa/pgxtest .
The test requires the test runner to fill in the credentials and location for a test database at the top of the file. The test database is expected to already be created.
Expected behavior
The program is expected to output the values :
1001
,1011
,[1110]
and[1100]
for the first database row2002
,2022
,[2220]
and[2200]
for the second database rowActual behavior
The program outputs the following values;
0
,1011
,[ ]
and[1100]
for the first database row0
,2022
,[ ]
, and[2200]
for the second database rowVersion
go version go1.22.4 windows/amd64
v5.6.0
andv5.7.2
testedThe text was updated successfully, but these errors were encountered: