From 4b5464c1de15efa473974950f758f1a5db16edd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Garcia?= Date: Tue, 29 Oct 2024 00:06:24 -0300 Subject: [PATCH] Fix `insertWithLastInsertID()` so it also works with *string IDs --- ksql.go | 6 ++++++ test_adapters.go | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/ksql.go b/ksql.go index 03a56f2..dbb49d0 100644 --- a/ksql.go +++ b/ksql.go @@ -540,6 +540,12 @@ func (c DB) insertWithLastInsertID( // we cannot retrieve it, so we just return: return nil + case reflect.Pointer: + if fieldType.Elem().Kind() == reflect.String { + return nil + } + + fallthrough default: return fmt.Errorf( "can't convert last insert id of type int64 into field `%s` of type %v", diff --git a/test_adapters.go b/test_adapters.go index 7fdca45..37d5a89 100644 --- a/test_adapters.go +++ b/test_adapters.go @@ -808,6 +808,33 @@ func InsertTest( tt.AssertEqual(t, result.Address, u.Address) }) + t.Run("should insert one user correctly when the ID is a pointer to string", func(t *testing.T) { + c := newTestDB(db, dialect) + + type ptrUser struct { + Name *string `ksql:"name"` + Address address `ksql:"address,json"` + } + name := "FernandaIsTheID" + u := ptrUser{ + Name: &name, + Address: address{ + Country: "Brazil", + }, + } + + err := c.Insert(ctx, NewTable("users", "name"), &u) + tt.AssertNoErr(t, err) + tt.AssertEqual(t, u.Name, &name) + + result := user{} + err = getUserByName(db, dialect, &result, "FernandaIsTheID") + tt.AssertNoErr(t, err) + + tt.AssertEqual(t, &result.Name, u.Name) + tt.AssertEqual(t, result.Address, u.Address) + }) + t.Run("should insert ignoring the ID with multiple ids", func(t *testing.T) { if dialect.InsertMethod() != sqldialect.InsertWithLastInsertID { return