Describe the problem
It is not possible to scan a TEXT ARRAY database column into a []string struct field.
To Reproduce
type Foo struct {
Bars []string `json:"bars"`
}
CREATE TABLE IF NOT EXISTS foos (
id BIGSERIAL NOT NULL,
bars TEXT ARRAY NOT NULL,
PRIMARY KEY (id)
);
...
for rows.Next() {
var foo Foo
if err := rows.Scan(&foo.Id, &foo.Bars); err != nil {
return Foo{}, err
}
return foo, nil
}
md5-cb1a9438c4b0904d634e0c528a522e8d
sql: Scan error on column index 1: unsupported Scan, storing driver.Value type []uint8 into type *[]string
Environment:
Additional context
I suppose this error does not come from the driver, since i could not find any such issue in the lib/pq repo...
I think this error comes from Go's database/sql. I'm not sure what the cause is, but I'm fairly sure that this isn't an issue with CockroachDB.
@jordanlewis Thanks for the hint!
In fact the driver needs assistance with array types:
...
for rows.Next() {
var foo Foo
if err := rows.Scan(&foo.Id, pq.Array(&foo.Bars)); err != nil {
return Foo{}, err
}
return foo, nil
}
@cgebe no problem, glad you figured it out!
Bars pq.StringArray json:"bars"
dengsibao , you da man! That's exactly what was needed!
Most helpful comment
Bars pq.StringArray
json:"bars"