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

Is there an equivalent to *gorm.DB.NewRecord() that was in GORM 1? #3400

Closed
Robbie-Perry opened this issue Sep 5, 2020 · 9 comments
Closed
Assignees
Labels

Comments

@Robbie-Perry
Copy link

Robbie-Perry commented Sep 5, 2020

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)?

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Sep 5, 2020
@github-actions
Copy link

github-actions bot commented Sep 5, 2020

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 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

@github-actions github-actions bot added status:stale type:question general questions and removed type:missing reproduction steps missing reproduction steps labels Sep 5, 2020
@jinzhu
Copy link
Member

jinzhu commented Sep 6, 2020

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.

@github-actions github-actions bot added type:missing reproduction steps missing reproduction steps and removed type:question general questions labels Sep 8, 2020
@Robbie-Perry
Copy link
Author

@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).

@github-actions
Copy link

github-actions bot commented Sep 8, 2020

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 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

@jinzhu
Copy link
Member

jinzhu commented Sep 9, 2020

@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).

So if no primary key exist, no operation will perform?

@Robbie-Perry
Copy link
Author

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.

@github-actions
Copy link

github-actions bot commented Sep 9, 2020

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 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

@jinzhu
Copy link
Member

jinzhu commented Sep 10, 2020

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.

usually the upsert / insert ignore could handle most cases, if this is required, maybe write a wrapper based on the API listed here https://gorm.io/docs/write_plugins.html

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

@jinzhu jinzhu closed this as completed Sep 10, 2020
@atz
Copy link

atz commented Oct 9, 2021

Here's some dev's workaround combining .Attrs() and a non-gorm struct field:
https://www.youtube.com/watch?v=JhsgV1RsIpQ

It's not super robust, mainly solves a use case in testing.

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

3 participants