Skip to content

sql: Scan error on column index 2, name "col3": unsupported Scan, storing driver.Value type []string into type *sql.RawBytes #164

@exfly

Description

@exfly

GORM Playground Link

go-gorm/playground#1

package main

import (
	"fmt"

	"gorm.io/driver/clickhouse"
	"gorm.io/gorm"
)

type User struct {
	Uniqid uint8  `gorm:"column:col1"`
	Name   string `gorm:"column:col2"`
}

func (User) TableName() string {
	return "user"
}

func main() {
	dsn := "clickhouse://user:passwd@localhost:9010/test?dial_timeout=10s&read_timeout=20s"
	rawDB, err := gorm.Open(clickhouse.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}

	rawDB.Exec("DROP TABLE IF EXISTS user")
	err = rawDB.Exec(`
	CREATE TABLE user (
		  col1 UInt8
		, col2 String
		, col3 Array(String)
	) Engine = Memory
`).Error
	if err != nil {
		panic(err)
	}

	u := User{
		Uniqid: 1,
		Name:   "gorm",
	}

	err = rawDB.Table("user").Create(&u).Error
	if err != nil {
		panic(err)
	}

	var raw []User
	err = rawDB.Table("user").Select("col1,col2").Find(&raw).Error
	if err != nil {
		panic(err) // works !!!
	}

	err = rawDB.Table("user").Find(&raw).Error
	if err != nil {
		panic(err) // panic: sql: Scan error on column index 2, name "col3": unsupported Scan, storing driver.Value type []string into type *sql.RawBytes
	}

	fmt.Println("Ok")
}

Description

If a non-base type exists in the table, Find will ret err:

`panic: sql: Scan error on column index 2, name "col3": unsupported Scan, storing driver.Value type []string into type *sql.RawBytes`

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions