mirror of https://github.com/VinGarcia/ksql.git
Fix `insertWithLastInsertID()` so it also works with *string IDs
parent
dcee1a01a4
commit
4b5464c1de
6
ksql.go
6
ksql.go
|
@ -540,6 +540,12 @@ func (c DB) insertWithLastInsertID(
|
||||||
// we cannot retrieve it, so we just return:
|
// we cannot retrieve it, so we just return:
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
|
case reflect.Pointer:
|
||||||
|
if fieldType.Elem().Kind() == reflect.String {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fallthrough
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"can't convert last insert id of type int64 into field `%s` of type %v",
|
"can't convert last insert id of type int64 into field `%s` of type %v",
|
||||||
|
|
|
@ -808,6 +808,33 @@ func InsertTest(
|
||||||
tt.AssertEqual(t, result.Address, u.Address)
|
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) {
|
t.Run("should insert ignoring the ID with multiple ids", func(t *testing.T) {
|
||||||
if dialect.InsertMethod() != sqldialect.InsertWithLastInsertID {
|
if dialect.InsertMethod() != sqldialect.InsertWithLastInsertID {
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue