RowToStructByName, Lax and Pos not working with `db:"-"` tag
- Dominant language
- Go
- Stars
- 14.3k
- Forks
- 1.1k
- Avg merge
- 6d 9h
- Merged PRs (30d)
- 11
Description
pgx throwing "struct doesn't have corresponding row field " when attempting to use struct tag `db:"-"` with the following functions:
- RowToStructByName
- RowToStructByNameLax
- RowToStructByPos
Code Im working with
**Model**
```go
// model
type AccountModel struct {
SkId uuid.UUID `db:"-"`
SkUsername string `db:"username"`
SkEmail string `db:"email"`
SkCreatedAt time.Time `db:"created_at"`
SkUpdatedAt time.Time `db:"updated_at"`
}
```
**Postgres Table**
```sql
CREATE TABLE "accounts" (
id UUID PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email TEXT NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL
);
```
**Queries used**
```go
// both queries were used in testing
queryv1 := `SELECT * FROM "accounts" WHERE id=$1 OR username=$2 OR email=$3`
queryv2 := `SELECT id,username,email,created_at,updated_at FROM "accounts" WHERE id=$1 OR username=$2 OR email=$3`
```
**Code being ran**
```go
type storage struct {
Conn *pgx.Conn
}
func (s *storage) Find(ctx context.Context, id uuid.UUID, email, username string) (AccountModel, error) {
rows, err := s.Conn.Query(ctx, queryv1, id, username, email) // both queryv1 and queryv2 was tried here
if err != nil {
return AccountModel{}, err
}
// doesn't work with
// RowToStructByName
// RowToStructByNameLax
// RowToStructByPos
m, err := pgx.CollectOneRow(rows, pgx.RowToStructByName[AccountModel])
if err != nil {
return AccountModel{}, err
}
return m, nil
}
```
**Error Message**
```
"struct doesn't have corresponding row field id"
```
**Expected behavior**
Should expect the id field to be excluded in the returned struct.
When I include the id field it works and throws no error.
I've looked through all previous error post opened and the solutions linked there have not worked.
**Version**
- Go: go version go1.22.5 windows/amd64
- PostgreSQL: PostgreSQL 16.1, compiled by Visual C++ build 1937, 64-bit
- pgx: github.com/jackc/pgx/v5 v5.6.0
Contributor guide
Research direction
Start by reproducing the error with RowToStructByName, RowToStructByNameLax, and RowToStructByPos using the provided AccountModel and queries. Read the implementations and their struct-tag handling, then add focused tests; done means db:"-" excludes the field without requiring a matching row column for all three functions.
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