Skip to content

Commit

Permalink
Merge pull request #209 from erizocosmico/docs/clarify-remove-notempty
Browse files Browse the repository at this point in the history
clarify the relationship to remove must not be empty
  • Loading branch information
erizocosmico authored Jul 26, 2017
2 parents f22b0a1 + 33a2594 commit 507e6de
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions benchmarks/kallax.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion generator/cli/kallax/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion generator/templates/model.tgo
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/kallax.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 507e6de

Please sign in to comment.