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

fix: nested scan (#6136) #6143

Closed
wants to merge 5 commits into from
Closed

Conversation

black-06
Copy link
Contributor

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

fix nested scan panic #6136

User Case Description

var user User
if err := DB.Scopes(func(db *gorm.DB) *gorm.DB {
	var maxID int64
	if err := db.Raw("select max(id) from users").Scan(&maxID).Error; err != nil {
		return db
	}
	return db.Raw("select * from users where id = ?", maxID)
}).Scan(&user).Error; err != nil {
	t.Errorf("failed to find user, got err: %v", err)
}

statement.go Outdated Show resolved Hide resolved
Copy link
Member

@a631807682 a631807682 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ok for me, but I think since we implemented a list, it would be a bit wasteful if we can only deal with bools. Of course, I don't think of other scenarios that need to be used at the moment, so I think it's ok.

@jinzhu
Copy link
Member

jinzhu commented Apr 11, 2023

If this PR is only used to fix the panic issue in the nested scan, I think don't panic but return an error should be enough.

We don't expect users to use it like this, for more details: https://gorm.io/docs/method_chaining.html

Going this close this PR for now, feel free to create a new one if those changes are required in other scenarios.

Thank you.

@jinzhu jinzhu closed this Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants