storm icon indicating copy to clipboard operation
storm copied to clipboard

Update does not update the struct passed as argument?

Open tgirod opened this issue 5 years ago • 1 comments

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 avatar Jul 02 '19 13:07 tgirod

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.

zebox avatar Jul 11 '19 06:07 zebox