Gorm: Update returning the updated struct fields

Created on 24 Nov 2015  路  2Comments  路  Source: go-gorm/gorm

Is there way to do something like this?

userID := int64(2941)

var user User

if err := DB.Where(&User{ID: userID}).First(&user).Error; err != nil {
    log.Fatalf("In %s: Failed to load user %v from SQL. Error (%v)", C.FuncPrint(), userID, err)
}

// log.Printf("%d", user.Balance)
// 1000

updatebalance := map[string]interface{}{"balance":  gorm.Expr("balance + ?", 1000)}

if err := DB.Model(user).UpdateColumns(updatebalance).Error; err != nil {
    log.Fatal(err)
}

Is there any way to get user.Balance updated to new value (returning) without re-selecting the object?

question

Most helpful comment

Is it possible to obtain IDs of the updated rows in one query? smth like this:
UPDATE table SET column = 'new value' WHERE column = 'old value' RETURNING table.id

Thanks!

All 2 comments

Is it possible to obtain IDs of the updated rows in one query? smth like this:
UPDATE table SET column = 'new value' WHERE column = 'old value' RETURNING table.id

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

littletwolee picture littletwolee  路  3Comments

superwf picture superwf  路  3Comments

bramp picture bramp  路  3Comments

kumarsiva07 picture kumarsiva07  路  3Comments

rfyiamcool picture rfyiamcool  路  3Comments