Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
exfly opened this issue Mar 25, 2024 · 2 comments
Assignees

Comments

@exfly
Copy link

exfly commented Mar 25, 2024

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`
@exfly
Copy link
Author

exfly commented Mar 25, 2024

temp workaround:

rawDB, err := gorm.Open(clickhouse.Open(dsn), &gorm.Config{
    QueryFields: true,
})

In addition, if you want to use the array, you can consider the following custom types:

type StringArray []string

func (a *StringArray) Scan(value interface{}) error {
	*a, _ = value.([]string)
	return nil
}

func (a StringArray) Value() (driver.Value, error) {
	val, err := json.Marshal([]string(a))
	if err != nil {
		return nil, err
	}
	return val, nil
}

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

@zhaojia123
Copy link

你的可以用?dsn := "clickhouse://username:pwd#@host:9000/database?dial_timeout=10s&read_timeout=20s"
rawDB, err := gorm.Open(clickhouse.Open(dsn), &gorm.Config{})
if err != nil {
fmt.Print("failed to connect database")
}
fmt.Print(rawDB)为啥 就这两行 都链接不上啊,官方的就行 他娘嘞 处死后 都错误 failed to initialize database,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants