uptrace/bun

Msgpack tag does not work for sqlite database

Aperta

#1219 aperta il 17 giu 2025

 (0 commenti) (0 reazioni) (0 assegnatari)Go (279 fork)user submission
bughelp wanted

Metriche repository

Star
 (4826 stelle)
Metriche merge PR
 (Merge medio 76g 17h) (1 PR mergiata in 30 g)

Descrizione

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

Guida contributor