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.
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
Most helpful comment
However, this seems to work:
... By splitting it up like that, it seems to leave both accessible?