#336·squirrel

Conditional insert column/value results in invalid SQL

Author: SouthclawsCreated Oct 5, 2022Updated Apr 8, 2026

I want to conditionally add a value to an insert query but the resulting SQL isn't valid.

go
	insert := squirrel.Insert("table")

	insert = insert.Columns(
		"id",
		"account_id",
	).Values(
		id,
		accountID,
	)

	if v, ok := inv.SomeValue.Get(); ok {
		insert = insert.Columns("some_value").Values(v)
	}

results in

sql
INSERT INTO table (id,account_id,some_value) VALUES ($1,$2),($3)

Is this a bug or do I just have to build a mutable slice of both keys and values and wire it up myself?

version github.com/Masterminds/squirrel v1.5.3