uptrace/bun

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

Open

#1,243 建立於 2025年7月24日

在 GitHub 查看
 (9 留言) (0 反應) (0 負責人)Go (4,826 star) (279 fork)user submission
bughelp wanted

描述

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!

貢獻者指南