mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-08-29 20:42:35 +00:00
fix: scan relation preloads through configured model
Tests / Unit Tests (push) Failing after 46s
Build , Vet Test, and Lint / Lint Code (push) Successful in 2m48s
Tests / Integration Tests (push) Failing after 19s
Build , Vet Test, and Lint / Run Vet Tests (1.24.x) (push) Successful in 2m56s
Build , Vet Test, and Lint / Run Vet Tests (1.23.x) (push) Successful in 2m58s
Build , Vet Test, and Lint / Build (push) Failing after 1m32s
Tests / Unit Tests (push) Failing after 46s
Build , Vet Test, and Lint / Lint Code (push) Successful in 2m48s
Tests / Integration Tests (push) Failing after 19s
Build , Vet Test, and Lint / Run Vet Tests (1.24.x) (push) Successful in 2m56s
Build , Vet Test, and Lint / Run Vet Tests (1.23.x) (push) Successful in 2m58s
Build , Vet Test, and Lint / Build (push) Failing after 1m32s
This commit is contained in:
@@ -565,9 +565,7 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
|
||||
logger.Debug("Querying single record with FetchRowNumber ID: %s", targetID)
|
||||
}
|
||||
|
||||
// For single record, create a new pointer to the struct type
|
||||
singleResult := reflect.New(modelType).Interface()
|
||||
pkName := reflection.GetPrimaryKeyName(singleResult)
|
||||
pkName := reflection.GetPrimaryKeyName(model)
|
||||
|
||||
query = query.Where(fmt.Sprintf("%s = ?", common.QuoteIdent(pkName)), targetID)
|
||||
|
||||
@@ -580,12 +578,21 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
|
||||
}
|
||||
query = hookCtx.Query
|
||||
|
||||
if err := query.Scan(ctx, singleResult); err != nil {
|
||||
// Scan through the model configured on the query. Bun rejects Scan with
|
||||
// a destination when the query preloads a has-many relation.
|
||||
if err := query.ScanModel(ctx); err != nil {
|
||||
logger.Error("Error querying record: %v", err)
|
||||
statusCode, errCode, errMsg = http.StatusInternalServerError, "query_error", "Error executing query"
|
||||
return err
|
||||
}
|
||||
result = singleResult
|
||||
|
||||
// The configured model is a slice so the same query construction works
|
||||
// for both collection and single-record reads. Extract its one result.
|
||||
scannedResults := reflect.ValueOf(modelPtr).Elem()
|
||||
if scannedResults.Len() == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
result = scannedResults.Index(0).Interface()
|
||||
} else {
|
||||
logger.Debug("Querying multiple records")
|
||||
|
||||
@@ -598,8 +605,9 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
|
||||
}
|
||||
query = hookCtx.Query
|
||||
|
||||
// Use the modelPtr already created and set on the query
|
||||
if err := query.Scan(ctx, modelPtr); err != nil {
|
||||
// Use the model already configured on the query. This is required by
|
||||
// Bun whenever the query includes a has-many preload.
|
||||
if err := query.ScanModel(ctx); err != nil {
|
||||
logger.Error("Error querying records: %v", err)
|
||||
statusCode, errCode, errMsg = http.StatusInternalServerError, "query_error", "Error executing query"
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user