From 50178a786f5832782319055106e3e32e7c8db89e Mon Sep 17 00:00:00 2001 From: AlisinaDevelo Date: Wed, 1 Jul 2026 03:50:40 +0200 Subject: [PATCH] rows: match db tag column names case-insensitively --- rows.go | 2 +- rows_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/rows.go b/rows.go index 4e5cf95d0..ad83afd86 100644 --- a/rows.go +++ b/rows.go @@ -843,7 +843,7 @@ func fieldPosByName(fldDescs []pgconn.FieldDescription, field string, normalize return i } } else { - if desc.Name == field { + if strings.EqualFold(desc.Name, field) { return i } } diff --git a/rows_test.go b/rows_test.go index 4cda957fc..05d8675c3 100644 --- a/rows_test.go +++ b/rows_test.go @@ -702,6 +702,24 @@ func TestRowToStructByNameDbTags(t *testing.T) { }) } +// A db tag must match its column case-insensitively. PostgreSQL folds unquoted +// identifiers to lower case, so a column aliased `as Region` comes back as +// "region" and has to match a `db:"Region"` tag. See +// https://github.com/jackc/pgx/issues/2296. +func TestRowToStructByNameDbTagsCaseInsensitive(t *testing.T) { + type record struct { + Region string `db:"Region"` + } + + defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) { + rows, _ := conn.Query(ctx, `select 'east' as Region`) + slice, err := pgx.CollectRows(rows, pgx.RowToStructByName[record]) + require.NoError(t, err) + require.Len(t, slice, 1) + assert.Equal(t, "east", slice[0].Region) + }) +} + func TestRowToStructByNameEmbeddedStruct(t *testing.T) { type Name struct { Last string `db:"last_name"`