Skip to content

Use a struct for named args #2406

@mxey

Description

@mxey

Is your feature request related to a problem? Please describe.

When querying data with pgx, I can quite easily load the data into structs using the various RowTo functions.
However, when inserting data, I have to repeat field names a lot:

_, err := c.db.Exec(ctx, `
	INSERT INTO foo (col)
	VALUES (@col)
`, pgx.StrictNamedArgs{
	"col":                 m.Col,
})

Describe the solution you'd like

A feature similar to (Strict)NamedArgs, but instead of passing a map, passing a struct with the appropriate struct tags:

type Foo struct {
	Col string `db:"col"`
}

_, err := c.db.Exec(ctx, `
	INSERT INTO foo (col)
	VALUES (@col)
`, pgx.StructArgs(m))

Describe alternatives you've considered

None.

Additional context

None.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions