asdine / storm

Simple and powerful toolkit for BoltDB
MIT License
2.07k stars 139 forks source link

Field as bool, Update failed #278

Closed zgwit closed 4 years ago

zgwit commented 4 years ago

As title.

My model: `

type User struct {

ID   int      `storm:"increment" json:"id"`

Name string   `json:"name"`

Disabled bool `json:"disabled"`

}

`

  1. Modify Disabled as false
  2. Call db.Update(&user)
  3. Call db.All(&users), Disabled is always true
asdine commented 4 years ago

Update only updates non-zero value fields, you need to use UpdateField. https://github.com/asdine/storm#update-an-object

zgwit commented 4 years ago

Operation:

true -> false !0 -> 0 "xxx" -> ""

Must use UpdateField ?

zgwit commented 4 years ago

unreasonable

asdine commented 4 years ago

Must use UpdateField ?

Yes.

unreasonable

Closing this issue as this answers the question and that the response is rude. Don't hesitate to open it if you need more info AND if you change your behavior.

skaldesh commented 4 years ago

I had the same problem right now. Could we update the part of the README that you linked (https://github.com/asdine/storm#update-an-object) so that it says so directly? Something like:

Update an object
// Update multiple fields
// Only works for non-zero value fields
err := db.Update(&User{ID: 10, Name: "Jack", Age: 45})

// Update a single field
// Also works for zero-value fields (0, false, "", ...)
err := db.UpdateField(&User{ID: 10}, "Age", 0)
skaldesh commented 4 years ago

I created a PR

asdine commented 4 years ago

Thank you @skaldesh! Sorry I didn't see the notification on this issue