-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Save (probably) should be idempotent #6139
Comments
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the |
I am experiencing the same issue. Here is the same issue: |
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the |
According to https://gorm.io/docs/update.html#Save-All-Fields
"Save is a combination function. If save value does not contain primary key, it will execute Create, otherwise it will execute Update (with all fields)."
The library is working as described in the quote, strictly speaking. But Save is not idempotent, and I may obviously be wrong but I don't think it is intuitive or convenient.
According to this code snippet at the end of the Save function :
If I call the Save function twice with the same object (assuming this object has an ID), the second time it is called no rows will be updated (because all fields keep the same value), thus the Create function will be called and likely provoke an error since the object already exists.
Obviously, I could append a pull request to the issue, simply deleting the if and its content, which should make the Save function idempotent, but I guess this code has a reason to be there. In case everything is working as intended, I fail to see a simple, clean and convenient way to perform a "if primary key is set, update else create". I looked into FirstOrCreate with Assign but I don't understand how to use it in a generic way. Is there something I am missing?
Regards
The text was updated successfully, but these errors were encountered: