Gorm: Update to RowsAffected and Error for updated with db.Save

Created on 13 Feb 2015  路  4Comments  路  Source: go-gorm/gorm

Hi,

I am wanting to be able updated a record by id, and verify if that id exists and resulted in a succesful update. I have found that I can get rows affected if I access directly from the save.

    okay = s.db.Save(product).RowsAffected > 0
    err = s.db.Error // This is always nil

But then the error is nil even if an error is raised, alternatively I can get the error back, but not the rows affected.

    err = s.db.Save(product).Error
    okay = s.db.RowsAffected > 0 // This is always zero

Is there a way I can use the API to get both these values back?

Thanks,
Julian.

Most helpful comment

However, this seems to work:

query := db.Exec("SELECT 1;");
rowsAffected, err := query.RowsAffected, query.Error;

... By splitting it up like that, it seems to leave both accessible?

All 4 comments

Hello @nativematch

Could you write a sample that could reproduce your issue? I just tested it, seems works for me.

Maybe caused by the product is not a pointer in your code, then it should be &product

But anyway, please reopen this issue if you could provide a reproduce-able code, you could use below template for that

package main

import (
    "log"

    "github.com/jinzhu/gorm"
    _ "github.com/mattn/go-sqlite3"
)

func main() {
    db, err := gorm.Open("sqlite3", "db.db")
    if err != nil {
        log.Fatal(err)
    }
}

I'm also having this issue. RowsAffected is 0 if you check the error.

However, this seems to work:

query := db.Exec("SELECT 1;");
rowsAffected, err := query.RowsAffected, query.Error;

... By splitting it up like that, it seems to leave both accessible?

@benguild tks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

satb picture satb  路  3Comments

hypertornado picture hypertornado  路  3Comments

sredxny picture sredxny  路  3Comments

pjebs picture pjebs  路  3Comments

alanyuen picture alanyuen  路  3Comments