-
-
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
Is there an equivalent to *gorm.DB.NewRecord() that was in GORM 1? #3400
Comments
This issue has been automatically marked as stale as it missing playground pull request link, checkout https://github.com/go-gorm/playground for details, it will be closed in 2 days if no further activity occurs. if you are asking question, please use the |
Hello, Could you let me know your use case that need to know it is a new record or not? There were many people thought GORM will check database to check if the record is new or not, to avoid the misunderstanding, we removed the method. We haven't list some changes like this one that could be caught by the compiler in the release note, but let me know your use case first. Thank you. |
@jinzhu I have a use case where some code handles arbitrary data types, where the primary key is unknown. I used NewRecord() to see if the primary keys were set on the item before performing an update (since doing an update on an item with empty keys would instead create a new one). |
This issue has been automatically marked as stale as it missing playground pull request link, checkout https://github.com/go-gorm/playground for details, it will be closed in 2 days if no further activity occurs. if you are asking question, please use the |
So if no primary key exist, no operation will perform? |
Or more so, the behaviour differs depending on whether or not there is a primary key present. |
This issue has been automatically marked as stale as it missing playground pull request link, checkout https://github.com/go-gorm/playground for details, it will be closed in 2 days if no further activity occurs. if you are asking question, please use the |
usually the I didn't saw a strong requirement currently, so compare to the confusion it caused and the benefit, I prefer to not have it right now |
Here's some dev's workaround combining It's not super robust, mainly solves a use case in testing. |
Your Questio n
In GORM 1, there was a function to check if a record was new, by seeing if its primary key was set. This does not exist anymore in GORM 2.
The document you expected this should be explained
It doesn't seem to be in the changelog of breaking changes.
Expected answer
Is there an equivalent function, or a new way of achieving this (without knowing about the actual model being checked)?
The text was updated successfully, but these errors were encountered: