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

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

Closed
lovejoy opened this issue Oct 16, 2024 · 6 comments
Closed
Assignees
Labels
type:missing reproduction steps missing reproduction steps

Comments

@lovejoy
Copy link

lovejoy commented Oct 16, 2024

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 github-actions bot added the type:missing reproduction steps missing reproduction steps label Oct 16, 2024
Copy link

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
Copy link
Contributor

ivila commented Oct 17, 2024

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.

Copy link

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
Copy link
Author

lovejoy commented Oct 17, 2024

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

Copy link

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
Copy link
Contributor

ivila commented Oct 17, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:missing reproduction steps missing reproduction steps
Projects
None yet
Development

No branches or pull requests

3 participants