diff --git a/README.md b/README.md index 760b8b3..81882e0 100644 --- a/README.md +++ b/README.md @@ -394,6 +394,22 @@ For one to one relationships: err := store.RemoveThing(user) ``` +Note that for that to work, the thing you're deleting must **not** be empty. That is, you need to eagerly load (or set afterwards) the relationships. + +```go +user, err := store.FindOne(NewUserQuery()) +checkErr(err) + +// THIS WON'T WORK! We've not loaded "Things" +err := store.RemoveThings(user) + +user, err := store.FindOne(NewUserQuery().WithThings()) +checkErr(err) + +// THIS WILL WORK! +err := store.RemoveThings(user) +``` + ## Query models ### Simple queries diff --git a/benchmarks/kallax.go b/benchmarks/kallax.go index b730740..2b4676f 100644 --- a/benchmarks/kallax.go +++ b/benchmarks/kallax.go @@ -327,6 +327,9 @@ func (s *PersonStore) Transaction(callback func(*PersonStore) error) error { // RemovePets removes the given items of the Pets field of the // model. If no items are given, it removes all of them. // The items will also be removed from the passed record inside this method. +// Note that is required that `Pets` is not empty. This method clears the +// the elements of Pets in a model, it does not retrieve them to know +// what relationships the model has. func (s *PersonStore) RemovePets(record *Person, deleted ...*Pet) error { var updated []*Pet var clear bool diff --git a/generator/cli/kallax/cmd.go b/generator/cli/kallax/cmd.go index 542ae96..8880a53 100644 --- a/generator/cli/kallax/cmd.go +++ b/generator/cli/kallax/cmd.go @@ -8,7 +8,7 @@ import ( "gopkg.in/urfave/cli.v1" ) -const version = "1.2.18" +const version = "1.2.19" func main() { newApp().Run(os.Args) diff --git a/generator/templates/model.tgo b/generator/templates/model.tgo index ff60d0e..b0c72ec 100644 --- a/generator/templates/model.tgo +++ b/generator/templates/model.tgo @@ -62,7 +62,7 @@ func (r *{{.Name}}) SetRelationship(field string, rel interface{}) error { {{else}}r.{{.Name}} = *val{{end}} return nil {{else}}case "{{.Name}}": - records, ok := rel.([]kallax.Record) + records, ok := rel.([]kallax.Record) if !ok { return fmt.Errorf("kallax: relationship field %s needs a collection of records, not %T", field, rel) } @@ -508,6 +508,9 @@ func (s *{{.StoreName}}) Transaction(callback func(*{{.StoreName}}) error) error // Remove{{.Name}} removes the given items of the {{.Name}} field of the // model. If no items are given, it removes all of them. // The items will also be removed from the passed record inside this method. +// Note that is required that `{{.Name}}` is not empty. This method clears the +// the elements of {{.Name}} in a model, it does not retrieve them to know +// what relationships the model has. func (s *{{.Model.StoreName}}) Remove{{.Name}}(record *{{.Model.Name}}, deleted ...{{if $.IsPtrSlice .}}*{{end}}{{$.GenTypeName .}}) error { var updated []{{if $.IsPtrSlice .}}*{{end}}{{$.GenTypeName .}} var clear bool diff --git a/tests/kallax.go b/tests/kallax.go index 8b075c4..7d16c44 100644 --- a/tests/kallax.go +++ b/tests/kallax.go @@ -3956,6 +3956,9 @@ func (s *ParentStore) Transaction(callback func(*ParentStore) error) error { // RemoveChildren removes the given items of the Children field of the // model. If no items are given, it removes all of them. // The items will also be removed from the passed record inside this method. +// Note that is required that `Children` is not empty. This method clears the +// the elements of Children in a model, it does not retrieve them to know +// what relationships the model has. func (s *ParentStore) RemoveChildren(record *Parent, deleted ...*Child) error { var updated []*Child var clear bool @@ -4555,6 +4558,9 @@ func (s *ParentNoPtrStore) Transaction(callback func(*ParentNoPtrStore) error) e // RemoveChildren removes the given items of the Children field of the // model. If no items are given, it removes all of them. // The items will also be removed from the passed record inside this method. +// Note that is required that `Children` is not empty. This method clears the +// the elements of Children in a model, it does not retrieve them to know +// what relationships the model has. func (s *ParentNoPtrStore) RemoveChildren(record *ParentNoPtr, deleted ...Child) error { var updated []Child var clear bool @@ -5228,6 +5234,9 @@ func (s *PersonStore) Transaction(callback func(*PersonStore) error) error { // RemovePets removes the given items of the Pets field of the // model. If no items are given, it removes all of them. // The items will also be removed from the passed record inside this method. +// Note that is required that `Pets` is not empty. This method clears the +// the elements of Pets in a model, it does not retrieve them to know +// what relationships the model has. func (s *PersonStore) RemovePets(record *Person, deleted ...*Pet) error { var updated []*Pet var clear bool @@ -6670,6 +6679,9 @@ func (s *QueryFixtureStore) RemoveRelation(record *QueryFixture) error { // RemoveNRelation removes the given items of the NRelation field of the // model. If no items are given, it removes all of them. // The items will also be removed from the passed record inside this method. +// Note that is required that `NRelation` is not empty. This method clears the +// the elements of NRelation in a model, it does not retrieve them to know +// what relationships the model has. func (s *QueryFixtureStore) RemoveNRelation(record *QueryFixture, deleted ...*QueryRelationFixture) error { var updated []*QueryRelationFixture var clear bool