From 8beaef87ba8a4923a84f75114c586e1429affcc6 Mon Sep 17 00:00:00 2001 From: Andy Day Date: Mon, 18 Jul 2022 20:36:39 -0700 Subject: [PATCH] dialect/sql/builder: make sql.NotIn() with empty args fallback to NOT FALSE (#2757) * dialect/sql/builder: make sql.NotIn() with empty args fallback to False() This is basically the identical change to #2735, but for NotIn(). This bug currently prevents anyone using NotIn() from upgrading from v0.10.x to v0.11.x * Update go.sum untidy * feedback --- dialect/sql/builder.go | 4 +++- dialect/sql/builder_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) 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())),