GORM Playground Link

https://github.sheincorp.cn/go-gorm/playground/pull/1

Description

when gorm connect to a wrong dsn config, and ignore the open error handle, all after db exec don't have result error

DB, err = gorm.Open(mysql.Open(dsn), gormConfig) // dsn is wrong ,and the err is  failed to initialize database, got error dial tcp xxx:3306: i/o timeout
result := DB.Exec("SELECT 1")
if result.Error != nil { // why the result error is nil 
    fmt.Println("%v", err)
}

I know i should handle the gorm.Open error. but i want to get my program startup when db is down.

so i tried to ignore the open error, but the db execute with no error result in some other issue. hope db execute can result error

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

When gorm.Open return an error(the second return value), the gorm.DB instance(the first return value) should never be used. Using an invalid gorm.DB instance is undefined behavior(as no one would ever do this) so the result could be anything(for example, in your case, return err=nil when calling Exec method). Hence, I think you should change your codes to handle the error properly, if there is an error returned from gorm.Open, stop using the returned gorm.DB instance.

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

When gorm.Open return an error(the second return value), the gorm.DB instance(the first return value) should never be used. Using an invalid gorm.DB instance is undefined behavior(as no one would ever do this) so the result could be anything(for example, in your case, return err=nil when calling Exec method). Hence, I think you should change your codes to handle the error properly, if there is an error returned from gorm.Open, stop using the returned gorm.DB instance.

I know i should handle the gorm.Open error. But my goal is just want to start my program as a downgrade solution even when the database cannot be connected

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

When gorm.Open return an error(the second return value), the gorm.DB instance(the first return value) should never be used. Using an invalid gorm.DB instance is undefined behavior(as no one would ever do this) so the result could be anything(for example, in your case, return err=nil when calling Exec method). Hence, I think you should change your codes to handle the error properly, if there is an error returned from gorm.Open, stop using the returned gorm.DB instance.

I know i should handle the gorm.Open error. But my goal is just want to start my program as a downgrade solution even when the database cannot be connected

Then in your downgrade solution, you must not use the gorm.DB instance