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

FirstOrCreate runs AfterFind hook and stops on error #4048

Closed
ghost opened this issue Feb 4, 2021 · 8 comments
Closed

FirstOrCreate runs AfterFind hook and stops on error #4048

ghost opened this issue Feb 4, 2021 · 8 comments
Assignees
Labels

Comments

@ghost
Copy link

ghost commented Feb 4, 2021

GORM Playground Link

go-gorm/playground#272

Description

The FirstOrCreate should actually check if there are any rows in the db, and if it exists then run AfterFind else create the row first.

In my case however the function returns error because it runs AfterFind() before creating the row, and thus panics "Unknown related entity for entity task : "
It does not run BeforeCreate at all

@ghost ghost assigned jinzhu Feb 4, 2021
@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Feb 4, 2021
@github-actions
Copy link

github-actions bot commented Feb 4, 2021

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

@ghost
Copy link
Author

ghost commented Feb 4, 2021

FYI... I think in FirstOrCreate the AfterFind is called even if there is no rows returned, and if we have any field updation on AfterFind() it gets done too : which gets reciprocated on BeforeCreate/BeforeSave.... I believe this is an unwanted behaviour @jinzhu

@github-actions
Copy link

github-actions bot commented Feb 4, 2021

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

@ghost
Copy link
Author

ghost commented Feb 5, 2021

go-gorm/playground#272

@ghost
Copy link
Author

ghost commented Feb 5, 2021

Firgured out this is a problem with Find as well as FirstOrCreate... Essentially AfterFind should not run if there are no rows affected after find query
@jinzhu

@github-actions
Copy link

github-actions bot commented Feb 5, 2021

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

@ghost
Copy link
Author

ghost commented Feb 5, 2021

go-gorm/playground#272

@github-actions github-actions bot added type:with reproduction steps with reproduction steps and removed type:missing reproduction steps missing reproduction steps labels Feb 5, 2021
@jinzhu jinzhu closed this as completed in 4373aa0 Feb 7, 2021
@jinzhu
Copy link
Member

jinzhu commented Feb 7, 2021

Fixed, thank you for your report.

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

1 participant