fix(db): guard against non-existent relations in preload
Some checks failed
Build , Vet Test, and Lint / Run Vet Tests (1.24.x) (push) Successful in -33m2s
Build , Vet Test, and Lint / Run Vet Tests (1.23.x) (push) Successful in -32m35s
Build , Vet Test, and Lint / Lint Code (push) Successful in -32m33s
Build , Vet Test, and Lint / Build (push) Successful in -32m50s
Tests / Integration Tests (push) Failing after -33m35s
Tests / Unit Tests (push) Successful in -33m22s

This commit is contained in:
Hein
2026-04-20 17:15:33 +02:00
parent 325769be4e
commit 0aaeff63a2

View File

@@ -597,6 +597,19 @@ func (b *BunSelectQuery) PreloadRelation(relation string, apply ...func(common.S
if !b.skipAutoDetect { if !b.skipAutoDetect {
model := b.query.GetModel() model := b.query.GetModel()
if model != nil && model.Value() != nil { if model != nil && model.Value() != nil {
// Guard against relations that don't exist on the model. Without this,
// bun panics inside Count/Scan with `model=X does not have relation="Y"`.
// Only validate the root segment so nested paths (e.g. "PRM.CHILD") still
// fall through to bun's native resolution.
rootRelation := relation
if idx := strings.Index(rootRelation, "."); idx >= 0 {
rootRelation = rootRelation[:idx]
}
if reflection.GetRelationType(model.Value(), rootRelation) == reflection.RelationUnknown {
logger.Warn("Skipping preload '%s': relation '%s' is not declared on model %T", relation, rootRelation, model.Value())
return b
}
relType := reflection.GetRelationType(model.Value(), relation) relType := reflection.GetRelationType(model.Value(), relation)
// Log the detected relationship type // Log the detected relationship type