jackc / jackc/pgx

RowToStructByName does not scan into embedded structures

Open
#1,717 10 comments 3 reactions 0 assignees View on GitHub
bug
Dominant language
Go
Stars
14.3k
Forks
1.1k
Avg merge
6d 9h
Merged PRs (30d)
11

Description

**Describe the bug**
I get "struct doesn't have corresponding row field id" error when I try to do RowToStructByName because I have another field in nested structure with `db:"id"` tag
**To Reproduce**
```go
type Subscription struct {
ID int `db:"id"`
TariffAlias string `db:"tariff_alias"`
Tariff
}

type Tariff struct {
ID int `db:"id"`
Alias string `db:"alias"`
}

pgx.CollectOneRow(rows, pgx.RowToStructByName[Subscription])
```

```sql
select * from subscription as s join tariff t on t.alias = s.tariff_alias;
```
**Expected behavior**
I get the completed structure Subscription
**Actual behavior**
struct doesn't have corresponding row field id

**Version**
- Go: go version go1.21 darwin/amd64
- pgx: github.com/jackc/pgx/v5.3.1

**Additional context**
The problem can be corrected by adding zeroing of the field name in the fieldPosByName function, as a mark that it has already found a match in the structure `fldDescs[i].Name = ""`

Contributor guide

Open the contributing guide

Research direction

Start with the fieldPosByName function mentioned in the issue and reproduce the behavior using the Subscription and Tariff structs and the shown query. Trace how duplicate db:"id" fields are matched; done means RowToStructByName returns the completed nested structure without the missing-row-field error.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, postgresql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.