Closed aimuz closed 4 years ago
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 RowsAffected
So 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 RowsAffected
So 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
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 RowsAffected
So 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
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 这个是个全局的,应该使用ret.RowsAffected (ret 是每一次操作单独调用结果)
{"cost":0.003117164,"file":"/www/host/blockchain-price/src/infra/repo/currency_repo.go:77","level":"info","msg":"","request_id":"iZwz9heg0pec2ut18ixomcZ.1663682053.1572222602964398080","rows":0,"sql":"UPDATE
currencySET
day_change=-4.930000,
day_change_amount=-0.084000,
high_price=1.920000,
low_price=1.583000,
price=1.620000,
update_time='2022-09-20 21:54:14.409' WHERE
symbol= 'NEBL'","time":"2022-09-20T21:54:14+08:00"} {"cost":0.003309807,"file":"/www/host/blockchain-price/src/infra/repo/currency_repo.go:77","level":"info","msg":"","request_id":"iZwz9heg0pec2ut18ixomcZ.1663682052.1572222599189524480","rows":1,"sql":"UPDATE
currencySET
day_change=-4.930000,
day_change_amount=-0.084000,
high_price=1.920000,
low_price=1.583000,
price=1.620000,
update_time='2022-09-20 21:54:13.595' WHERE
symbol= 'NEBL'","time":"2022-09-20T21:54:13+08:00"}
这是我日志记录的sql,第一个记得rows为0,第二个记录rows为1,这区间数据库没有任何改动,加上 update_time 是为了保证数据库有值就一定变更,结果是数据库变更了,RowsAffected还是为0
I am also experiencing this issue.
I'm also facing this issue, even though it actually inserts the records. Kindly note that for me this bug arises only during batch inserts.
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 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 这个是个全局的,应该使用ret.RowsAffected (ret 是每一次操作单独调用结果)
谢谢,解决了 Issue solved, thank you
It’s 2024 and this bug hasn’t been solved yet
When I run
// gorm gen query
d := query.Day
info, err := d.Where(d.ID.Eq(1)).Update(d.Status, 1)
sometimes info.RowsAffected
is 1
, sometimes is 0
. But even though the value is 0
, the database is updated successfully.
// wrong case
tx := xx.Begin()
tx.Update(xx).Error
fmt.Println(tx.RowsAffected) // result is 0
// correct case
tx := xx.Begin()
db := tx.Update(xx)
fmt.Println(db.RowsAffected) // result is n
Transaction update RowsAffected is 0
Just like the following code, when it updates, RowsAffected is 0
What version of Go are you using (
go version
)?go version go1.10.3 darwin/amd64
Which database and its version are you using?
mysql 5.7