Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clarify the relationship to remove must not be empty #209

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
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
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
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
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
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