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

Scan row into multiple structs #6986

Open
elbek opened this issue Apr 22, 2024 · 1 comment
Open

Scan row into multiple structs #6986

elbek opened this issue Apr 22, 2024 · 1 comment
Assignees
Labels
type:question general questions

Comments

@elbek
Copy link

elbek commented Apr 22, 2024

How to scan a row into multiple structs

The document you expected this should be explained

This code used to work with 1.20 go version

func MultiScan(db *gorm.DB, rows *sql.Rows, items ...interface{}) error {
	for _, item := range items {
		err := db.ScanRows(rows, item)
		if err != nil {
			return err
		}
	}
	return nil
}

It fails to work now giving error scan called without calling next (closemuscanhold) in 1.22

Expected answer

How can we scan a row into multiple struct.
Something like this didn't work:

func MultiScan1(db *gorm.DB, rows *sql.Rows, items ...interface{}) error {
	var records []any
	records = append(records, items...)
	err := db.ScanRows(rows, &records)
	if err != nil {
		return err
	}
	return nil
}

I know I can create a struct and embed all these structs and scan into it, but this is no go for us. We have hundred of places and scan scans into 5-10 structs often.

Looking a way to fix multi scan in a way that works.

@elbek elbek added the type:question general questions label Apr 22, 2024
@elbek
Copy link
Author

elbek commented May 7, 2024

@jinzhu Any idea how can we accomplish this?

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

No branches or pull requests

2 participants