Skip to content
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

Closed
haroldbouley opened this issue Mar 10, 2023 · 3 comments
Closed

Save (probably) should be idempotent #6139

haroldbouley opened this issue Mar 10, 2023 · 3 comments
Labels

Comments

@haroldbouley
Copy link

haroldbouley commented Mar 10, 2023

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 :

updateTx := tx.callbacks.Update().Execute(tx.Session(&Session{Initialized: true}))

if updateTx.Error == nil && updateTx.RowsAffected == 0 && !updateTx.DryRun && !selectedUpdate {
	return tx.Create(value)
}

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

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Mar 10, 2023
@github-actions
Copy link

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 Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@polly3d
Copy link

polly3d commented Mar 13, 2023

I am experiencing the same issue.
Just added a 'created_at' field now.

Here is the same issue:
6134

@github-actions
Copy link

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 Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

black-06 added a commit to black-06/gorm that referenced this issue Mar 14, 2023
jinzhu pushed a commit that referenced this issue Mar 23, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants