Your Question

Gorm: v1.25.5 Go: 1.21.4 MySQL: 8.0.31

A similar issue from https://github.com/go-gorm/gorm/issues/2038, and use the test case from it. Please refer to the following screenshot.

Gorm Transaction update RowsAffected is 0

So even if there are 12 rows which have "name == 5" in database (statement 1 in screenshot), but the transaction update RowsAffected is 0 (statement 2 in screenshot).

The SQL "UPDATE user_tests SET password = '12345' WHERE (name = '5')" works well in MySQL Console and "12 rows affected".

The document you expected this should be explained

Expected answer

It is a normal scenario or a bug?

Thank you.

Comment From: ivila

I think you might misunderstand the behavior of MySQL(or any other SQL servers). By reading your codes and your console output of your snapshot, I think there is no bug in gorm, but misunderstanding on your side.

At first, you have 12 records that they are all the same, password is '12345', and name is '5'.

Then, you execute update query, set password to '12345' where name is '5', when executing, MySQL did found 12 rows with name '5', but it won't change anything as you are updating name to what it is now. so the result of mysql server returned should be Rows matched: 12 Changed: 0 Warnings: 0 (I don't think it works well in MySQL Console and "12 rows affected")

Last, you execute update query, set password to '44444' where name is '4', nothing matched, so nothing changed.

Everything works as expected on both MySQL server and gorm, you may need to check your mysql console as it works well in MySQL Console and "12 rows affected"

Comment From: JamesYuan

@ivila Thank you the details. I have understood the reasons.

Thanks.