uptrace/bun

Msgpack tag does not work for sqlite database

Ouverte

#1 219 ouverte le 17 juin 2025

 (0 commentaire) (0 réaction) (0 personne assignée)Go (279 forks)user submission
bughelp wanted

Métriques du dépôt

Stars
 (4 826 étoiles)
Métriques de merge PR
 (Merge moyen 76j 17h) (1 PR mergée en 30 j)

Description

Hi! I've encountered issues when using msgpack struct tag with sqlite database:

Repo with code to reproduce

package main

import (
	"database/sql"
	"testing"

	"github.com/stretchr/testify/require"
	"github.com/uptrace/bun"
	"github.com/uptrace/bun/dialect/sqlitedialect"
	"github.com/uptrace/bun/driver/sqliteshim"
)

type Item struct {
	Something int `msgpack:"something"`
}

type Model struct {
	bun.BaseModel `bun:"table:models"`

	ID      int    `bun:",pk,autoincrement"`
	Name    string `bun:",notnull"`
	Encoded Item   `bun:",msgpack"`
}

func TestBunMsgpack(t *testing.T) {
	sqldb, err := sql.Open(sqliteshim.ShimName, ":memory:")
	require.NoError(t, err)
	sqldb.SetMaxIdleConns(1000)
	sqldb.SetConnMaxLifetime(0)

	db := bun.NewDB(sqldb, sqlitedialect.New())
	db.NewCreateTable().
		Model(&Model{}).
		Exec(t.Context())

	model := &Model{
		Name:    "test",
		Encoded: Item{Something: 1},
	}
	_, err = db.NewInsert().Model(model).Returning("id").Exec(t.Context(), &model.ID)
	require.NoError(t, err)

	model2 := &Model{}
	err = db.NewSelect().Model(model2).Where("id = ?", model.ID).Scan(t.Context())
	require.NoError(t, err)

	require.Equal(t, model, model2)
}

Running go test . gives following error:

--- FAIL: TestBunMsgpack (0.00s)
    main_test.go:45: 
                Error Trace:    /home/leksus/test/main_test.go:45
                Error:          Received unexpected error:
                                sql: Scan error on column index 2, name "encoded": msgpack: unexpected code=5c decoding map length
                Test:           TestBunMsgpack
FAIL
FAIL    example 0.006s
FAIL

Looks like the underlying issue is the same as in #519 ? If so, maybe it's worth documenting that msgpack is deprecated / should not be used outside of the postgres? Right now it's not clear at all that this is the case: https://bun.uptrace.dev/guide/models.html#struct-tags

Guide contributeur