Home  >  Article  >  Backend Development  >  Scan error on column index 8, name 'replicated': Scan not supported, storing driver.Value of type uint8 as type **bool

Scan error on column index 8, name 'replicated': Scan not supported, storing driver.Value of type uint8 as type **bool

PHPz
PHPzforward
2024-02-12 15:15:07848browse

列索引 8 上的扫描错误,名称“replicated”:不支持扫描,将 driver.Value 类型 uint8 存储为 **bool 类型

Question content

I use sqlx to select PostgreSQL boolan[] into a Golang structure, where the target structure value is []*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": Scan is not supported, driver.Value type []uint8 is stored as type *[]*bool

I've looked around but haven't found a solution yet. Any help would be greatly appreciated!

Solution

You can only scan content that implements the .Scanner interface. You can define the structure 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 []bool, or if you really need it to be []*bool you can create your own type

type BoolArray []*bool

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

The above is the detailed content of Scan error on column index 8, name 'replicated': Scan not supported, storing driver.Value of type uint8 as type **bool. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete