-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RowsAffected is incorrect on subsequent updates on mysql #7384
Comments
Are you trying to update the I think you should expect 3 rows found, not 3 rows affected. The MySQL Server (and I believe newer versions of PostgreSQL as well) might omit your second update if it determines that no actual changes are needed. You might just check: #6795 |
Yes the test is updating the rows to the same values. I didn’t find the closed issue when searching prior to raising this one.
If you run the SQL statements generated via MySQL workbench it reports that both statements affect 3 rows.
Having done some more reading I suspect that the Workbench is using the CLIENT_FOUND_ROWS setting, and that Gorm is not. https://dev.mysql.com/doc/c-api/8.4/en/mysql-affected-rows.html
Given the different results between Gorm and MySQL Workbench, it would be helpful if the documentation about RowsAffected and MySQL mentioned the setting CLIENT_FOUND_ROWS.
On 11 Mar 2025, at 4:19 pm, ZC ***@***.***> wrote:
Are you trying to update the birthday field with the same value it already has?
I think you should expect 3 rows found, not 3 rows affected.
The MySQL Server (and I believe newer versions of PostgreSQL as well) might omit your second update if it determines that no actual changes are needed.
You might just check: #6795<#6795>
—
Reply to this email directly, view it on GitHub<#7384 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ADD24XF34WSMRFUIRJJQPZ32TZ56TAVCNFSM6AAAAABYQ5JX7CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOMJSHAYDAOJZGI>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
[ivila]ivila left a comment (go-gorm/gorm#7384)<#7384 (comment)>
Are you trying to update the birthday field with the same value it already has?
I think you should expect 3 rows found, not 3 rows affected.
The MySQL Server (and I believe newer versions of PostgreSQL as well) might omit your second update if it determines that no actual changes are needed.
You might just check: #6795<#6795>
—
Reply to this email directly, view it on GitHub<#7384 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ADD24XF34WSMRFUIRJJQPZ32TZ56TAVCNFSM6AAAAABYQ5JX7CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOMJSHAYDAOJZGI>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
@jinzhu @a631807682 For reference: CLIENT_FOUND_ROWS |
|
No, I think we could add it to FAQ (as developers keep asking this repeatedly), how about this. Q: Why do I get unexpected results from RowsAffected? Sorry, my bad, I forgot to mention the comment section is in documentation, not in the code comments. |
In my opinion, it does not belong to the question that needs to be explained in the document. I think it is quickest to search through the issue and look at the comments. You can also move the question to https://github.com/go-gorm/gorm/discussions/categories/q-a |
This section in the documentation could be updated to cover the situation. The answer from ivila above could be in a Note so that developers will know that RowsAffected works differently on MySQL and MariaDB compared to the other DBMS' which return a combination of rows matched and rows updated. Please note that PostgreSQL returns the modified and matched count from an Update statement. |
GORM Playground Link
go-gorm/playground#793
Description
Execute an update Exec against a supported dbms and check the RowsAffected is the expected number.
Execute a slightly different (or the same) update Exec against the same table and check the RowsAffected is the expected number.
If there are 3 records that will, and are, updated, then RowsAffected should be populated with 3 each time you Exec the update.
Source data for this example is 3 user records with the age set to 25, and active set to true.
Example updates:
The text was updated successfully, but these errors were encountered: