uptrace/bun

Regression in v1.2.X: Nested Relation() fails when using struct composition for shared fields and relations

Chiusa

#1243 aperta il 24 lug 2025

 (9 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 run into a regression after upgrading from Bun v1.1.14 to v1.2.14 involving struct composition for shared fields and relationships.

I use composition in my entity definitions to embed common fields and relationships. Here's a simplified version of the setup:

Common fields:

type IDField struct {
	ID int64 `bun:",pk,unique,autoincrement"`
}

type AgentRelation struct {
	AgentID int64  `bun:",nullzero"`
	Agent   *Agent `bun:"rel:belongs-to,join:agent_id=id"`
}

type LeadRelation struct {
	LeadID int64 `bun:",nullzero"`
	Lead   *Lead `bun:"rel:belongs-to,join:lead_id=id"`
}

type BusinessRelation struct {
	BusinessID int64     `bun:",nullzero"`
	Business   *Business `bun:"rel:belongs-to,join:business_id=id"`
}

Models:

type Agent struct {
	IDField
	BusinessRelation

	Associations []*Link `bun:"rel:has-many,join:id=agent_id"`
}

type Business struct {
	IDField
	LeadRelation

	// other fields
}

type Lead struct {
	IDField
	BusinessRelation

	// other fields
}

type Link struct {
	IDField
	AgentRelation
	LeadRelation

        // other fields
}

Query:

var agents []entity.Agent
if err := db.NewSelect().
	Model(&agents).
	Relation("Associations.Lead.Business").
	Scan(ctx); err != nil {
	panic(err)
}

Result: It panics with

sql: Scan error on column index 6, name "lead__business__id": bun: Link does not have column "lead__business__id"

Possible Cause

The bug appears to be in how Bun v1.2 resolves embedded/composed structs during relation joins — particularly when chaining multiple levels (e.g. Agent → Link → Lead → Business)

Thanks in advance!

Guida contributor