Skip to content

NamedExec issues expanding VALUES #898

Open
@nicjohnson145

Description

@nicjohnson145

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

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.

	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.

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