Skip to content

Scanning nested structs implementing Scan and Value #618

@DallanQ

Description

@DallanQ

I'm wondering if it's possible to scan nested structs when the nested struct implements Scan and Value.

My database table definition is

create table test(
	id serial not null constraint test_pkey primary key,
	body jsonb,
	insert_time timestamp with time zone default CURRENT_TIMESTAMP,
	last_update_time timestamp with time zone default CURRENT_TIMESTAMP
);

My struct definitions are:

type Body struct {
	One string
	Two string
}
func (b Body) Scan(val interface{}) error {
	return json.Unmarshal(val.([]byte), &b)
}
func (b Body) Value() (driver.Value, error) {
	return json.Marshal(b)
}
type Wrapper struct {
	ID int64                 `db:"id"`
	Body                     `db:"body"`
	InsertTime time.Time     `db:"insert_time"`
	LastUpdateTime time.Time `db:"last_update_time"`
}

But the following code gives an error: non-struct dest type struct with >1 columns (4)

ws := []Wrapper{}
err = db.SelectContext(ctx, &ws, "SELECT * FROM test")

Am I trying to do something that's not possible, or do I have an error somewhere?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions