bun icon indicating copy to clipboard operation
bun copied to clipboard

Panic on has-many relation with join on byte array

Open iam047801 opened this issue 2 years ago • 1 comments

The example below panics with hash of unhashable type []uint8 runtime error on Select query.

Expected behavior: first block with second and third in it.

Reproduced on the latest version v1.1.11.

Can we fix it?

package main

import (
	"context"
	"database/sql"

	"github.com/uptrace/bun"
	"github.com/uptrace/bun/dialect/pgdialect"
	"github.com/uptrace/bun/driver/pgdriver"
)

type Block struct {
	bun.BaseModel `bun:"table:block_info"`

	Shard int64  `bun:",pk,notnull"`
	SeqNo uint32 `bun:",pk,notnull"`
	Hash  []byte `bun:"type:bytea,unique,notnull"`

	MasterHash []byte `bun:"type:bytea"`

	Shards []*Block `bun:"rel:has-many,join:hash=master_hash"`
}

func main() {
	dsnPG := "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable"

	sqlDB := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsnPG)))
	pg := bun.NewDB(sqlDB, pgdialect.New())

	ctx := context.Background()

	_, err := pg.NewDropTable().Model((*Block)(nil)).IfExists().Exec(ctx)
	if err != nil {
		panic(err)
	}
	_, err = pg.NewCreateTable().Model((*Block)(nil)).Exec(ctx)
	if err != nil {
		panic(err)
	}

	b := []*Block{{
		Shard: -1,
		SeqNo: 1000,
		Hash:  []byte("first"),
	}, {
		Shard:      0,
		SeqNo:      1000,
		Hash:       []byte("second"),
		MasterHash: []byte("first"),
	}, {
		Shard:      0,
		SeqNo:      1001,
		Hash:       []byte("third"),
		MasterHash: []byte("first"),
	}}

	_, err = pg.NewInsert().Model(&b).Exec(ctx)
	if err != nil {
		panic(err)
	}

	var res = new(Block)

	err = pg.NewSelect().Model(res).Relation("Shards").Where("hash = ?", b[0].Hash).Scan(ctx)
	if err != nil {
		panic(err)
	}
}
panic: runtime error: hash of unhashable type []uint8

goroutine 1 [running]:
github.com/uptrace/bun.baseValues.func1({0x10442bd20?, 0x14000090420?, 0x1400014fb98?})
        /Users/user/go/pkg/mod/github.com/uptrace/[email protected]/model_table_has_many.go:139 +0x114
github.com/uptrace/bun.visitField({0x10442bd20?, 0x14000090420?, 0x1046a45b8?}, {0x140000a60e0, 0x0, 0x1}, 0x1400014fc20)
        /Users/user/go/pkg/mod/github.com/uptrace/[email protected]/util.go:38 +0xe4
github.com/uptrace/bun.walk({0x10442bd20?, 0x14000090420?, 0x1400014fbe8?}, {0x140000a60e0, 0x0, 0x1}, 0xfbe8?)
        /Users/user/go/pkg/mod/github.com/uptrace/[email protected]/util.go:25 +0x8c
github.com/uptrace/bun.baseValues({0x104448348, 0x140000c60f0}, {0x140001326b8, 0x1, 0x1})
        /Users/user/go/pkg/mod/github.com/uptrace/[email protected]/model_table_has_many.go:136 +0x134
github.com/uptrace/bun.newHasManyModel(0x140000aa050)
        /Users/user/go/pkg/mod/github.com/uptrace/[email protected]/model_table_has_many.go:27 +0x7c
github.com/uptrace/bun.(*relationJoin).manyQuery(0x1400014fd48?, 0x140000ca1e0)
        /Users/user/go/pkg/mod/github.com/uptrace/[email protected]/relation_join.go:57 +0x28
github.com/uptrace/bun.(*relationJoin).selectMany(0x1400014fe78?, {0x1044476c8, 0x14000124000}, 0x104191b90?)
        /Users/user/go/pkg/mod/github.com/uptrace/[email protected]/relation_join.go:49 +0x2c
github.com/uptrace/bun.(*SelectQuery).selectJoins(0x140000ca000, {0x1044476c8, 0x14000124000}, {0x140000aa050, 0x1, 0x140000c9000?})
        /Users/user/go/pkg/mod/github.com/uptrace/[email protected]/query_select.go:473 +0x424
github.com/uptrace/bun.(*SelectQuery).Scan(0x140000ca000, {0x1044476c8, 0x14000124000}, {0x0?, 0x0?, 0x0?})
        /Users/user/go/pkg/mod/github.com/uptrace/[email protected]/query_select.go:888 +0x1a8
main.main()
        go-bun-bug/main.go:64 +0x6d4

iam047801 avatar Feb 09 '23 16:02 iam047801

got the same error with a table that has []byte field, have you found some kind of workaround?

Shanaiyaa avatar Mar 09 '23 23:03 Shanaiyaa