Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
NathanBaulch opened this issue Jan 13, 2025 · 1 comment · Fixed by #1108
Closed

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

NathanBaulch opened this issue Jan 13, 2025 · 1 comment · Fixed by #1108
Assignees

Comments

@NathanBaulch
Copy link
Contributor

NathanBaulch commented Jan 13, 2025

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)
}
@j2gg0s
Copy link
Collaborator

j2gg0s commented Jan 14, 2025

The changes in #1080 replaced the variable itself with the value returned by Value().

In this specific case, the variable itself is [16]byte, an array and can be used to compute a hash.
However, Value() returns a []byte, a slice, which cannot be used to compute a hash.

Thank you very much for your feedback. I will address this issue as soon as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants