postgresqlgosqlx

Scan error on column index 8, name "replicated": unsupported Scan, storing driver.Value type []uint8 into type *[]*bool


I am using sqlx to select a PostgreSQL boolan[] into a Golang struct where the target struct value is a []*bool.

type App struct {
    ApplicationID string  `db:"application_id"`
    Name          string  `db:"name"`
    Description   string  `db:"description"`
    Replicated    []*bool `db:"replicated"`
}

var apps []App

err := trx.Select(&apps, "Select * From my_function()")

The error returned is: sql: Scan error on column index 3, name "replicated": unsupported Scan, storing driver.Value type []uint8 into type *[]*bool

I have searched all around but have not been able to find a solution yet. Any help would be appreciated!


Solution

  • You can only scan into something that implements the .Scanner interface. You could define your struct as

    import "github.com/lib/pq"
    
    type App struct {
        ApplicationID string  `db:"application_id"`
        Name          string  `db:"name"`
        Description   string  `db:"description"`
        Replicated    pq.BoolArray `db:"replicated"`
    }
    

    where pq.BoolArray is an []bool, or if you really need it to be []*bool, you could create your own type

    type BoolArray []*bool
    

    and then copy the code from here https://github.com/lib/pq/blob/2a217b94f5ccd3de31aec4152a541b9ff64bed05/array.go#L76 and modify as needed