Transaction update RowsAffected is 0
Just like the following code, when it updates, RowsAffected is 0
go version
)?go version go1.10.3 darwin/amd64
mysql 5.7
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"log"
)
var db *gorm.DB
func init() {
var err error
db, err = gorm.Open("mysql", "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True")
if err != nil {
panic(err)
}
db.LogMode(true)
}
type UserTest struct {
gorm.Model
Name string
Password string
}
func main() {
ret := db.AutoMigrate(&UserTest{})
if ret.Error != nil {
log.Fatal(ret.Error)
}
user := &UserTest{
Name: "5",
Password: "2",
}
ret = db.Model(&UserTest{}).Create(user)
if ret.Error != nil {
log.Fatal(ret.Error)
}
var users []UserTest
ret = db.Model(&UserTest{}).Find(&users)
if ret.Error != nil {
log.Fatal(ret.Error)
return
}
for _, v := range users {
log.Println(v.Name, v.Password)
}
tx := db.Begin()
ret = tx.Table("user_tests").Where("name = ?", "5").Updates(map[string]interface{}{
"password": "12345",
})
if ret.Error != nil {
log.Fatal(ret.Error)
tx.Rollback()
return
}
log.Println(ret.RowsAffected)
ret = tx.Table("user_tests").Where("name = ?", "4").Updates(map[string]interface{}{
"password": "44444",
})
if ret.Error != nil {
log.Fatal(ret.Error)
tx.Rollback()
return
}
log.Println(ret.RowsAffected)
tx.Commit()
// 重新查
ret = db.Model(&UserTest{}).Find(&users)
if ret.Error != nil {
log.Fatal(ret.Error)
return
}
for _, v := range users {
log.Println(v.Name, v.Password)
}
}
I can't see what the problem (but am stupid), can u provide more detail if possible..
Is this a bug?
Yes, I think it's a Bug, and this bug's problem leads to the fact that when you update a record the same as a database, you don't get a return record.
The most important thing is that this Bug's lead occurs only when the transaction occurs, and when the update command is executed separately, the number of rows that can be updated is obtained. I'm puzzled
@aimuz no data with name = 4
I got the same problem. I find the affected in db debug log is right
UPDATE `orders` SET `status` = 2, `updated_at` = '2019-07-11 19:08:08' WHERE (order_no = 155765990288785421 and status = 1)
[1 rows affected or returned ]
But zero (not right) in return:
tx.Model(&tradeModel.Order{}).
Where("order_no = ? and status = ?", orderNo, int16(pb.OrderStatus_OrderStatusWaitConfirm)).
Update("status", int16(status))
log.Println(tx.RowsAffected) // is zero
I don't know if this project is stopped.
This is affecting me too. Even for delete within a transaction.
I got the same problem
Same problem also for me on
go version go1.13.1 linux/amd64
and
github.com/jinzhu/gorm v1.9.11
i have the same. db log says one row affected but db.AffectedRows is zero.
serious bug
this issue isn't solved?
I have same issue.
So I tried to send query at mysql cli, is working(value updated and rows affected is correct).
Yes, it hasn't been solved yet
For others coming to this thread, I could verify this works for sqlite3 and postgres.
if ret.RowsAffected == 0 {
tx.Rollback()
return fmt.Errorf("no record found for id %s and status CREATED", _feedback.Id)
} else if ret.RowsAffected > 1 {
tx.Rollback()
return fmt.Errorf("update affected %d rows, maximum allowed = 1", ret.RowsAffected)
}
me too
have plan to fix this bug? or there is aother way to know the RowsAffected. Thanks a lot.
It happened to me too.
Your question does not match your description
I got the same problem. I find the affected in db debug log is right
UPDATE `orders` SET `status` = 2, `updated_at` = '2019-07-11 19:08:08' WHERE (order_no = 155765990288785421 and status = 1) [1 rows affected or returned ]
But zero (not right) in return:
tx.Model(&tradeModel.Order{}). Where("order_no = ? and status = ?", orderNo, int16(pb.OrderStatus_OrderStatusWaitConfirm)). Update("status", int16(status)) log.Println(tx.RowsAffected) // is zero
tx.RowsAffected Incorrect usage
ret = tx.Model(&tradeModel.Order{}).
Where("order_no = ? and status = ?", orderNo, int16(pb.OrderStatus_OrderStatusWaitConfirm)).
Update("status", int16(status))
log.Println(ret.RowsAffected)
ret.RowsAffected != tx.RowsAffected
tx.RowsAffected Incorrect usage
tx not RowsAffected
tx.RowsAffected Incorrect usage
tx not RowsAffected
So how to use it
It seems that I'm not alone in this problem
tx.RowsAffected Incorrect usage
tx not RowsAffectedSo how to use it
It seems that I'm not alone in this problem
tx.RowsAffected Incorrect usage
ret = tx.Model(&tradeModel.Order{}).
Where("order_no = ? and status = ?", orderNo, int16(pb.OrderStatus_OrderStatusWaitConfirm)).
Update("status", int16(status))
log.Println(ret.RowsAffected)
ret.RowsAffected != tx.RowsAffected
ret replace tx
tx.RowsAffected Incorrect usage
tx not RowsAffectedSo how to use it
It seems that I'm not alone in this problemtx.RowsAffected Incorrect usage
ret = tx.Model(&tradeModel.Order{}).
Where("order_no = ? and status = ?", orderNo, int16(pb.OrderStatus_OrderStatusWaitConfirm)).
Update("status", int16(status))
log.Println(ret.RowsAffected)ret.RowsAffected != tx.RowsAffected
ret replace tx
I don't think you understand me
Although I haven't used it for a long time, by checking my history
There are two updates in the transaction. The first update can get the rowsaffected, while the second update cannot get the correct rowsaffected
If I were to ask questions at that time, I might have more details.
tx.RowsAffected Incorrect usage
tx not RowsAffectedSo how to use it
It seems that I'm not alone in this problemtx.RowsAffected Incorrect usage
ret = tx.Model(&tradeModel.Order{}).
Where("order_no = ? and status = ?", orderNo, int16(pb.OrderStatus_OrderStatusWaitConfirm)).
Update("status", int16(status))
log.Println(ret.RowsAffected)
ret.RowsAffected != tx.RowsAffected
ret replace txI don't think you understand me
Although I haven't used it for a long time, by checking my history
There are two updates in the transaction. The first update can get the rowsaffected, while the second update cannot get the correct rowsaffected
If I were to ask questions at that time, I might have more details.
从你的截图里面看,是你使用的方法不对
不能使用 tx.RowsAffected 这个是个全局的,应该使用ret.RowsAffected (ret 是每一次操作单独调用结果)