uptrace/bun

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

Fermée

#1 243 ouverte le 24 juil. 2025

 (9 commentaires) (0 réaction) (0 personne assignée)Go (279 forks)user submission
bughelp wanted

Métriques du dépôt

Stars
 (4 826 étoiles)
Métriques de merge PR
 (Merge moyen 76j 17h) (1 PR mergée en 30 j)

Description

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!

Guide contributeur