stephenafamo/bob

Conditionally using IN for SQL queries

scubadiver214 opened this issue · 1 comments

Hi - we are running into a potential issue where we pass an empty slice and it crashes..

e.g.

models.foo.IN(someCriteria) results in an Err condition.

Should IN filter for nil or an empty slice?

As a workaround, i am just conditionally not adding an IN for the query predicate.

I think the source code surrounding this is:

func (w WhereMod[Q, C]) In(slice ...C) mods.Where[Q] {
	values := make([]any, 0, len(slice))
	for _, value := range slice {
		values = append(values, value)
	}
	return mods.Where[Q]{E: w.name.In(Arg(values...))}
}

If you add an "IN" mod, Bob will try to build it, so I can imagine it may create an invalid query if there are no values in the slice.

Your current method of not adding the IN predicate is the way to go. Alternatively, you can create a custom bob.Mod to make this easier.