diff --git a/dialect/sql/builder.go b/dialect/sql/builder.go index f8252775e..4d15f628f 100644 --- a/dialect/sql/builder.go +++ b/dialect/sql/builder.go @@ -1587,8 +1587,10 @@ func NotIn(col string, args ...interface{}) *Predicate { // NotIn appends the `Not IN` predicate. func (p *Predicate) NotIn(col string, args ...interface{}) *Predicate { + // If no arguments were provided, append the NOT FALSE constant, since + // we cannot apply "NOT IN ()". This will make this predicate truthy. if len(args) == 0 { - return p + return Not(p.False()) } return p.Append(func(b *Builder) { b.Ident(col).WriteOp(OpNotIn) diff --git a/dialect/sql/builder_test.go b/dialect/sql/builder_test.go index 5688f421a..7c2899691 100644 --- a/dialect/sql/builder_test.go +++ b/dialect/sql/builder_test.go @@ -635,6 +635,16 @@ func TestBuilder(t *testing.T) { wantQuery: `DELETE FROM "users" WHERE "parent_id" IS NULL AND "name" NOT IN ($1, $2)`, wantArgs: []interface{}{"foo", "bar"}, }, + { + input: Delete("users"). + Where(And(IsNull("parent_id"), In("name"))), + wantQuery: "DELETE FROM `users` WHERE `parent_id` IS NULL AND FALSE", + }, + { + input: Delete("users"). + Where(And(IsNull("parent_id"), NotIn("name"))), + wantQuery: "DELETE FROM `users` WHERE `parent_id` IS NULL AND (NOT (FALSE))", + }, { input: Delete("users"). Where(And(False(), False())),