https://github.com/go-gorm/gorm/blob/52404cddbb5f5b18253cebe12fde20b577b1c86d/gorm.go#L136 When an error occurs in Apply and exits, if AfterInitialize added via defer is executed, the passed db is nil. Using db will trigger a panic, masking the error from Apply.

Comment From: github-actions[bot]

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.io ✨ Search Before Asking

Comment From: ydzh

Issue Description:

Description:

In the Apply function, if an error occurs during its execution and it exits, there's a potential issue when the deferred function AfterInitialize is executed afterward. In this scenario, if the Apply function exits with an error and AfterInitialize is called, the passed db parameter is nil. Consequently, attempting to use db within AfterInitialize will trigger a panic, potentially masking the original error from the Apply function.

Steps to Reproduce:

Execute Apply function with a scenario that triggers an error. Ensure that AfterInitialize is added via defer and is called after Apply exits. Attempt to access db within AfterInitialize. Expected Behavior:

AfterInitialize should handle the nil db parameter gracefully, avoiding a panic, and allowing the original error from the Apply function to be properly handled.

Actual Behavior:

When attempting to use db within AfterInitialize after Apply has exited with an error, a panic occurs due to the nil db parameter, potentially masking the original error.

Proposed Solution:

Ensure that AfterInitialize handles a nil db parameter gracefully to prevent panics. This could involve adding a nil check for db within AfterInitialize or modifying the design to avoid using db if it might be nil.

Comment From: ydzh

from chatgpt

Comment From: github-actions[bot]

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.io ✨ Search Before Asking