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

StructScan into array of string #877

Open
dennissetiawan opened this issue Jul 14, 2023 · 0 comments
Open

StructScan into array of string #877

dennissetiawan opened this issue Jul 14, 2023 · 0 comments

Comments

@dennissetiawan
Copy link

I have this snippet of code that works correctly when scanning varchar[] into []string in postgresql

type OrderDB struct {
	ID                string               `db:"id"`
	Users     []string             `db:"users"`
}

rows, err := tx.QueryxContext(ctx, query, args...)
	if err != nil {
		return nil, tracerr.Wrap(err)
	}
	defer rows.Close()

	var orderDB OrderDB

	for rows.Next() {
		err = rows.Scan(
			&orderDB.ID,
			pq.Array(&orderDB.Users),
		)
		if err != nil {
			return nil, tracerr.Wrap(err)
		}

	}

But i want to use StructScan instead, what should i do?
I know that we can make an alias for []string and implement the Scan method like

type StringArrayDB []string
func (u *StringArrayDB) Scan(v interface{}) error {
	return pq.GenericArray{A: u}.Scan(v)
}

but i still got error like
pq: parsing array element index 0: pq: scanning to string is not implemented; only sql.Scanner,

what should i do? and i think this should be handled in structscan?

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

No branches or pull requests

1 participant