Golang查询扫描未将查询正确扫描到结构中
我在从 Golang 中的 pgx 查询进行扫描时遇到问题。 id 字段始终是最后一条记录的字段。如果我取消注释函数顶部的 var person Person 声明,则每个 id 都是 3。我的数据库中有 3 条 id 为 1 到 3 的记录。当我评论该声明并在 rows.Next()
循环中声明变量时,我得到了正确的 id。我不明白为什么 personId
没有被
函数顶部声明的 var 正确覆盖来自编组 JSON 的输出。
[{"person_id":3,"first_name":"马克","last_name":"布朗"},{"person_id":3,"first_name":"山姆","last_name":"史密斯" },{"person_id":3,"first_name":"鲍勃","last_name":"琼斯"}]
在扫描循环的每次迭代中声明 person 后的输出
[{"person_id":1,"first_name":"马克","last_name":"布朗"},{"person_id":2,"first_name":"山姆","last_name":"史密斯" },{"person_id":3,"first_name":"鲍勃","last_name":"琼斯"}]
我有这个结构
// Person model
type Person struct {
PersonId *int64 `json:"person_id"`
FirstName *string `json:"first_name"`
LastName *string `json:"last_name"`
}
这是我的查询函数
func getPersons(rs *appResource, companyId int64) ([]Person, error) {
// var person Person
var persons []Person
queryString := `SELECT
user_id,
first_name,
last_name,
FROM users
WHERE company_id = $1`
rows, err := rs.db.Query(context.Background(), queryString, companyId)
if err != nil {
return persons, err
}
for rows.Next() {
var person Person
err = rows.Scan(
&person.PersonId,
&person.FirstName,
&person.LastName)
if err != nil {
return persons, err
}
log.Println(*person.PersonId) // 1, 2, 3 for both var patterns
persons = append(persons, person)
}
if rows.Err() != nil {
return persons, rows.Err()
}
return persons, err
}
I am having trouble with scanning from a pgx
query in Golang. The id field is always that of the last record. If I un-comment the var person Person declaration at the top of the function, every id is 3. There are 3 records with id's from 1 to 3 in my db. When I comment that declaration and declare the variable in the rows.Next()
loop I get the correct id's. I can't figure out why the personId
isn't being correctly overwritten
output from marshalled JSON with the var declared at the top of the function.
[{"person_id":3,"first_name":"Mark","last_name":"Brown"},{"person_id":3,"first_name":"Sam","last_name":"Smith"},{"person_id":3,"first_name":"Bob","last_name":"Jones"}]
output after declaring person every iteration of the scan loop
[{"person_id":1,"first_name":"Mark","last_name":"Brown"},{"person_id":2,"first_name":"Sam","last_name":"Smith"},{"person_id":3,"first_name":"Bob","last_name":"Jones"}]
I have this struct
// Person model
type Person struct {
PersonId *int64 `json:"person_id"`
FirstName *string `json:"first_name"`
LastName *string `json:"last_name"`
}
Here is my query function
func getPersons(rs *appResource, companyId int64) ([]Person, error) {
// var person Person
var persons []Person
queryString := `SELECT
user_id,
first_name,
last_name,
FROM users
WHERE company_id = $1`
rows, err := rs.db.Query(context.Background(), queryString, companyId)
if err != nil {
return persons, err
}
for rows.Next() {
var person Person
err = rows.Scan(
&person.PersonId,
&person.FirstName,
&person.LastName)
if err != nil {
return persons, err
}
log.Println(*person.PersonId) // 1, 2, 3 for both var patterns
persons = append(persons, person)
}
if rows.Err() != nil {
return persons, rows.Err()
}
return persons, err
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您在 github.com/jackc/pgx/v4 中发现了一个错误(或者至少是意外行为)。运行
Scan
时,如果指针(例如person.PersonId
)不为零,那么它指向的任何内容都会被重用。为了证明这一点,我复制了该问题并确认您也可以通过以下方式修复它:我可以使用此简化代码复制该问题:
其输出为:
您会注意到指针在每种情况下都是相同的。我做了一些进一步的测试:
scanDst = nil
可以解决这个问题。database/sql
(使用"github.com/jackc/pgx/stdlib"
驱动程序)时,代码按预期工作。PersonId
是*string
(并且查询使用num::text
),则它会按预期工作。该问题似乎可以归结为
convert.go 中的以下内容
:因此这可以处理目标是指针的情况(对于某些数据类型)。该代码检查它是否为零,如果是,则创建相关类型的新实例作为目标。如果它不为零,它只会重用指针。注意:我已经有一段时间没有使用
reflect
了,所以我的解释可能存在问题。由于行为与
database/sql
不同并且可能会引起混乱,我认为这可能是一个错误(我猜这可能是减少分配的尝试)。我快速查看了这些问题,但找不到任何报告(稍后将进行更详细的查看)。I believe that you have discovered a bug (or, at least, unexpected behaviour) in
github.com/jackc/pgx/v4
. When runningScan
it appears that if the pointer (soperson.PersonId
) is not nil then whatever it is pointing to is being reused. To prove this I replicated the issue and confirmed that you can also fix it with:I can duplicate the issue with this simplified code:
The output from this is:
You will note that the pointer is the same in each case. I have done some further testing:
scanDst = nil
in the above fixes the issue.database/sql
(with the"github.com/jackc/pgx/stdlib"
driver) the code works as expected.PersonId
is*string
(and query usesnum::text
) it works as expected.The issue appears to boil down to the following in
convert.go
:So this handles the case where the destination is a pointer (for some datatypes). The code checks if it is nil and, if so, creates a new instance of the relevant type as a destination. If it's not nil it just reuses the pointer. Note: I've not used
reflect
for a while so there may be issues with my interpretation.As the behaviour differs from
database/sql
and is likely to cause confusion I believe it's probably a bug (I guess it could be an attempt to reduce allocations). I have had a quick look at the issues and could not find anything reported (will have a more detailed look later).