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

Update does not update the struct passed as argument? #251

Open
tgirod opened this issue Jul 2, 2019 · 1 comment
Open

Update does not update the struct passed as argument? #251

tgirod opened this issue Jul 2, 2019 · 1 comment

Comments

@tgirod
Copy link

tgirod commented Jul 2, 2019

If I understand things correctly, calling db.Save(&data) will alter the record in the database, and &data will contain the updated version.

On the other hand, if I call db.Update(&data), the non-zero fields gets updated in the database but &data won't reflect the state of the database.

Am I missing something? Is it on purpose?

@tgirod tgirod changed the title Update does not return the updated struct ? Update does not update the struct passed as argument? Jul 2, 2019
@zebox
Copy link

zebox commented Jul 11, 2019

In documentation Update function not get arguments as filled struct object. The arg of function look like as creating new struct object which values set on create.

// Update multiple fields
err := db.Update(&User{ID: 10, Name: "Jack", Age: 45})

// Update a single field
err := db.UpdateField(&User{ID: 10}, "Age", 0)

I temporary fix it so:

// Update object data in DB
func(obj *MyObject) UpdateMyObject(db *storm.DB) error {
	return db.Update(&MyObject{
		UID:               obj.UID,
		DetectTime:         obj .DetectTime,
		PassedTime:         obj .PassedTime,
		TotalDuration:      obj .TotalDuration,
		State:              obj .State,
	})
}

It approach doesn't look like good, but fix my issue with update data in DB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants