diff --git a/templates/16_update.tpl b/templates/16_update.tpl index 2732466..8b84989 100644 --- a/templates/16_update.tpl +++ b/templates/16_update.tpl @@ -52,6 +52,9 @@ func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string if !cached { wl := strmangle.UpdateColumnSet({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns, whitelist) + if len(wl) == 0 { + return errors.New("{{.PkgName}}: unable to update {{.Table.Name}}, could not build whitelist") + } cache.query = fmt.Sprintf("UPDATE {{$schemaTable}} SET %s WHERE %s", strmangle.SetParamNames("{{.LQ}}", "{{.RQ}}", {{if .Dialect.IndexPlaceholders}}1{{else}}0{{end}}, wl), @@ -63,10 +66,6 @@ func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string } } - if len(cache.valueMapping) == 0 { - return errors.New("{{.PkgName}}: unable to update {{.Table.Name}}, could not build whitelist") - } - values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping) if boil.DebugMode { diff --git a/templates/17_upsert.tpl b/templates/17_upsert.tpl index 6710f13..84c1f70 100644 --- a/templates/17_upsert.tpl +++ b/templates/17_upsert.tpl @@ -85,6 +85,9 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, {{if ne .DriverName {{$varNameSingular}}PrimaryKeyColumns, updateColumns, ) + if len(update) == 0 { + return errors.New("{{.PkgName}}: unable to upsert {{.Table.Name}}, could not build update column list") + } {{if ne .DriverName "mysql" -}} var conflict []string diff --git a/templates_test/singleton/boil_queries_test.tpl b/templates_test/singleton/boil_queries_test.tpl index 90419be..bd41389 100644 --- a/templates_test/singleton/boil_queries_test.tpl +++ b/templates_test/singleton/boil_queries_test.tpl @@ -35,3 +35,4 @@ func (f *fKeyDestroyer) Read(b []byte) (int, error) { return f.buf.Read(b) } + diff --git a/templates_test/update.tpl b/templates_test/update.tpl index 38aa71a..b33854d 100644 --- a/templates_test/update.tpl +++ b/templates_test/update.tpl @@ -5,6 +5,10 @@ func test{{$tableNamePlural}}Update(t *testing.T) { t.Parallel() + if len({{$varNameSingular}}Columns) == len({{$varNameSingular}}PrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + seed := randomize.NewSeed() var err error {{$varNameSingular}} := &{{$tableNameSingular}}{} @@ -31,24 +35,18 @@ func test{{$tableNamePlural}}Update(t *testing.T) { t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } - // If table only contains primary key columns, we need to pass - // them into a whitelist to get a valid test result, - // otherwise the Update method will error because it will not be able to - // generate a whitelist (due to it excluding primary key columns). - if strmangle.StringSliceMatch({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns) { - if err = {{$varNameSingular}}.Update(tx, {{$varNameSingular}}PrimaryKeyColumns...); err != nil { - t.Error(err) - } - } else { - if err = {{$varNameSingular}}.Update(tx); err != nil { - t.Error(err) - } + if err = {{$varNameSingular}}.Update(tx); err != nil { + t.Error(err) } } func test{{$tableNamePlural}}SliceUpdateAll(t *testing.T) { t.Parallel() + if len({{$varNameSingular}}Columns) == len({{$varNameSingular}}PrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + seed := randomize.NewSeed() var err error {{$varNameSingular}} := &{{$tableNameSingular}}{} diff --git a/templates_test/upsert.tpl b/templates_test/upsert.tpl index 4f25b1d..f9c351e 100644 --- a/templates_test/upsert.tpl +++ b/templates_test/upsert.tpl @@ -5,6 +5,10 @@ func test{{$tableNamePlural}}Upsert(t *testing.T) { t.Parallel() + if len({{$varNameSingular}}Columns) == len({{$varNameSingular}}PrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + seed := randomize.NewSeed() var err error // Attempt the INSERT side of an UPSERT