Skip to content

Regression - panic scanning model with custom PK type and has-many field #1107

@NathanBaulch

Description

@NathanBaulch

I'm no longer able to scan a model with a has-many relation included when using PK types that implement driver.Valuer. I'm using https://github.com/oklog/ulid for all PKs in my model.
This regression was introduced by #1080 in v1.2.7 and seen previously in #779. cc @thecampagnards @Tiscs

panic({0x207b680?, 0xc0000672c0?})
        .../scoop/apps/go/current/src/runtime/panic.go:785 +0x132
github.com/uptrace/bun.baseValues.func1({0x22fefe0?, 0xc0003d2280?, 0x132ffbe0108?})
        .../go/pkg/mod/github.com/uptrace/bun@v1.2.7/model_table_has_many.go:140 +0x136
github.com/uptrace/bun.visitField({0x22fefe0?, 0xc0003d2280?, 0xc000500008?}, {0xc0004a7840, 0x0, 0x1}, 0xc0008930b8)
        .../go/pkg/mod/github.com/uptrace/bun@v1.2.7/util.go:38 +0xcf
github.com/uptrace/bun.walk({0x22fefe0?, 0xc0003d2280?, 0x0?}, {0xc0004a7840, 0x0, 0x1}, 0xc0008930b8)
        .../go/pkg/mod/github.com/uptrace/bun@v1.2.7/util.go:25 +0x9f
github.com/uptrace/bun.baseValues({0x26def00, 0xc000896000}, {0xc00009a740, 0x1, 0x1})
        .../go/pkg/mod/github.com/uptrace/bun@v1.2.7/model_table_has_many.go:137 +0x1c7
github.com/uptrace/bun.newHasManyModel(0xc0007b1c70)
        .../go/pkg/mod/github.com/uptrace/bun@v1.2.7/model_table_has_many.go:28 +0x75
github.com/uptrace/bun.(*relationJoin).manyQuery(0xc0007b1c70, 0xc0007b8960)
        .../go/pkg/mod/github.com/uptrace/bun@v1.2.7/relation_join.go:57 +0x25
github.com/uptrace/bun.(*relationJoin).selectMany(0x92983a?, {0x26d0850, 0xc00055f6e0}, 0x0?)
        .../go/pkg/mod/github.com/uptrace/bun@v1.2.7/relation_join.go:49 +0x25
github.com/uptrace/bun.(*SelectQuery).selectJoins(0xc0007b8780, {0x26d0850, 0xc00055f6e0}, {0xc0007b1c70, 0x1, 0xc0007b8780?})
        .../go/pkg/mod/github.com/uptrace/bun@v1.2.7/query_select.go:447 +0x465
github.com/uptrace/bun.(*SelectQuery).scanResult(0xc0007b8780, {0x26d0850, 0xc00055f6e0}, {0x0?, 0x0?, 0x0?})
        .../go/pkg/mod/github.com/uptrace/bun@v1.2.7/query_select.go:831 +0x2d7
github.com/uptrace/bun.(*SelectQuery).Scan(...)
        .../go/pkg/mod/github.com/uptrace/bun@v1.2.7/query_select.go:793

Reproducible with (DB connection required):

type Parent struct {
	ID       ULID     `bun:",pk"`
	Children []*Child `bun:"rel:has-many,join:id=parent_id"`
}

type Child struct {
	ID       ULID `bun:",pk"`
	ParentID ULID
}

type ULID [16]byte
func (id *ULID) Scan(any) error              { return nil }
func (id ULID) Value() (driver.Value, error) { return []byte{}, nil }

func main() {
	db := bun.NewDB(sql.OpenDB(c), pgdialect.New())
	panic(db.NewSelect().Model(&Parent{}).Relation("Grants").Scan(ctx))
}

Alternatively reproduce with a unit test:

func TestBaseValues(t *testing.T) {
	db := &DB{fmter: schema.NewNopFormatter()}
	db.dialect = db.fmter.Dialect()
	mdl, err := _newModel(db, &Parent{}, true)
	assert.NoError(t, err)
	j := mdl.(TableModel).join("Children")
	assert.NotNil(t, j)
	baseValues(j.JoinModel, j.Relation.BasePKs)
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions