Skip to content

Conversation

@warkanum
Copy link

In model_table_has_many.go, the indirectAsKey method has a nil pointer bug.
Calling reflect.TypeOf(v).Kind() if v is nil causes a panic.

@warkanum warkanum changed the title Fix rare panic in indirectAsKey (model_table_has_many.go) when loading complex models fix: Rare panic in indirectAsKey (model_table_has_many.go) when loading complex models Nov 20, 2025
@Aoang
Copy link
Collaborator

Aoang commented Nov 26, 2025

Hi. It looks like the root cause here is an improper implementation of driver.Valuer. Could you elaborate on the circumstances under which this occurs?

Implementing a fix for this in the proposed way could inadvertently obscure issues and hinder the troubleshooting process for other incorrect driver.Valuer implementations down the line.

@Aoang Aoang self-requested a review November 26, 2025 11:20
@warkanum
Copy link
Author

warkanum commented Dec 18, 2025

Thanks for the update. Apologies for only responding now.
I agree that it is better for debugging. However, this happens when using sql.NullString or any of the sql.Null types.

When a string is invalid, a nil is returned. e.g.

type NullString struct {
	String string
	Valid  bool // Valid is true if String is not NULL
}
func (ns NullString) Value() (driver.Value, error) {
	if !ns.Valid {
		return nil, nil
	}
	return ns.String, nil
}

It happens with you have nested .Relation of 3 levels or deeper.

@warkanum
Copy link
Author

warkanum commented Dec 18, 2025

More detail on the example.

type ModelCoreMastertask struct {
	bun.BaseModel `bun:"table:core.mastertask,alias:mastertask"`

	Ridmastertask sql.NullInt64              `json:"rid_mastertask" bun:"rid_mastertask,type:integer,pk,default:nextval('core.identity_mastertask_rid_mastertask'::regclass),"`
	MAL           []*ModelCoreMastertaskitem `json:"MAL,omitempty" bun:"rel:has-many,join:rid_mastertask=rid_mastertask"`
}

type ModelCoreMastertaskitem struct {
	bun.BaseModel     `bun:"table:core.mastertaskitem,alias:mastertaskitem"`
	Ridmastertaskitem sql.NullInt64                   `json:"rid_mastertaskitem" bun:"rid_mastertaskitem,type:integer,pk,default:nextval('core.identity_mastertaskitem_rid_mastertaskitem'::regclass),"`
	Ridmastertask     sql.NullInt64                   `json:"rid_mastertask" bun:"rid_mastertask,type:integer,"`
	MTEV              []*ModelCoreMastertaskitemevent `json:"MTEV,omitempty" bun:"rel:has-many,join:rid_mastertaskitem=rid_mastertaskitem"`
}

type ModelCoreMastertaskitemevent struct {
	bun.BaseModel `bun:"table:core.mastertaskitemevent,alias:mastertaskitemevent"`

	Ridmastertaskitemevent sql.NullInt64            `json:"rid_mastertaskitemevent" bun:"rid_mastertaskitemevent,type:integer,pk,default:nextval('core.identity_mastertaskitemevent_rid_mastertaskitemevent'::regclass),"`
	Ridmastertaskitem      sql.NullInt64            `json:"rid_mastertaskitem" bun:"rid_mastertaskitem,type:integer,"`
	MAL                    *ModelCoreMastertaskitem `json:"MAL,omitempty" bun:"rel:has-one,join:rid_mastertaskitem=rid_mastertaskitem"`
}

When the foreign key is null or there are other sql.Null fields in the related models, that error occurs.
Nil return by the valuer.Value for sql.Null fields.

@Aoang
Copy link
Collaborator

Aoang commented Dec 19, 2025

Looks good.

You need to rewrite the commit message to meet lint checks. https://github.com/uptrace/bun/actions/runs/20338177597/job/58429992529?pr=1302#step:4:14

@warkanum
Copy link
Author

Thanks. I updated the commit message. 👍🏻

@Aoang Aoang merged commit ad853b0 into uptrace:master Dec 19, 2025
5 checks passed
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 this pull request may close these issues.

2 participants