go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
37.02k stars 3.94k forks source link

when gorm not connected to any DB; the query execute result has no error #7241

Closed lovejoy closed 1 month ago

lovejoy commented 1 month ago

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

github-actions[bot] commented 1 month ago

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.ioSearch Before Asking

ivila commented 1 month ago

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.

github-actions[bot] commented 1 month ago

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.ioSearch Before Asking

lovejoy commented 1 month ago

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

github-actions[bot] commented 1 month ago

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.ioSearch Before Asking

ivila commented 1 month ago

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