#898·sqlx

NamedExec issues expanding `VALUES`

Author: nicjohnson145Created Oct 12, 2023Updated Sep 6, 2025

When populating a values statement with multiple entries, certain query shapes are not expanding as expected. This can be shown with the following example

go
stmt := `
	INSERT INTO
		foo_table
	VALUES
	(
		:id,
		:foo,
		:bar
	)
`
args := []map[string]any{
	{
		"id": 1,
		"foo": "foo1",
		"bar": "bar1",
	},
	{
		"id": 2,
		"foo": "foo2",
		"bar": "bar2",
	},
}

_, err := db.NamedExec(stmt, args)

If foo_table has 3 columns (or any remaining columns are defaulted) then this is a legal insert statement. Instead this fails with an error similar to

pq: got 6 parameters but the statement requires 3

This is easy enough to work around in the case of an insert, just specify the columns. However this is impossible to work around in the case of an UPDATE ... FROM statement.

sql
	UPDATE
		foo_table f
	SET
		foo = v.foo
	FROM (
		VALUES
		(:id, :foo)
	) AS v ( id, foo )
	WHERE
		f.id = v.id

I believe this is due to the regex here which incorrectly requires that a values statement be preceeded by a closing parenthesis, so the values statements do not get duplicated in the above cases.