Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RowToStructByName, Lax and Pos not working with db:"-" tag #2093

Open
MiniJ147 opened this issue Jul 19, 2024 · 1 comment
Open

RowToStructByName, Lax and Pos not working with db:"-" tag #2093

MiniJ147 opened this issue Jul 19, 2024 · 1 comment
Labels

Comments

@MiniJ147
Copy link

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

// 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

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

// 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

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
@MiniJ147 MiniJ147 added the bug label Jul 19, 2024
@MiniJ147 MiniJ147 changed the title RowToStructByName, RowToStructByNameLax and RowToStructByPos not working with db:"-" tag RowToStructByName, Lax and Pos not working with db:"-" tag Jul 19, 2024
@jackc
Copy link
Owner

jackc commented Jul 22, 2024

Your query returns a column "id" but you don't have anywhere to scan it. Every column returned from a query must have a destination for the RowToStruct* functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants