uptrace/bun

Msgpack tag does not work for sqlite database

Open

#1,219 创建于 2025年6月17日

在 GitHub 查看
 (0 评论) (0 反应) (0 负责人)Go (4,826 star) (279 fork)user submission
bughelp wanted

描述

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

贡献者指南