fix(restheadspec): qualify primary key and sort columns with table alias

This commit is contained in:
Hein
2026-05-20 12:41:16 +02:00
parent 3d2e11eeed
commit 0647a88aba

View File

@@ -619,16 +619,19 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
logger.Debug("FetchRowNumber: Row number %d for PK %s = %s", rowNum, pkName, fetchRowNumberPKValue) logger.Debug("FetchRowNumber: Row number %d for PK %s = %s", rowNum, pkName, fetchRowNumberPKValue)
// Now filter the main query to this specific primary key // Now filter the main query to this specific primary key
query = query.Where(fmt.Sprintf("%s = ?", common.QuoteIdent(pkName)), fetchRowNumberPKValue) tableAlias := reflection.ExtractTableNameOnly(tableName)
query = query.Where(fmt.Sprintf("%s.%s = ?", common.QuoteIdent(tableAlias), common.QuoteIdent(pkName)), fetchRowNumberPKValue)
} else if id != "" { } else if id != "" {
// If ID is provided (and not FetchRowNumber), filter by ID // If ID is provided (and not FetchRowNumber), filter by ID
pkName := reflection.GetPrimaryKeyName(model) pkName := reflection.GetPrimaryKeyName(model)
logger.Debug("Filtering by ID=%s: %s", pkName, id) logger.Debug("Filtering by ID=%s: %s", pkName, id)
query = query.Where(fmt.Sprintf("%s = ?", common.QuoteIdent(pkName)), id) tableAlias := reflection.ExtractTableNameOnly(tableName)
query = query.Where(fmt.Sprintf("%s.%s = ?", common.QuoteIdent(tableAlias), common.QuoteIdent(pkName)), id)
} }
// Apply sorting // Apply sorting
tableAlias := reflection.ExtractTableNameOnly(tableName)
for _, sort := range options.Sort { for _, sort := range options.Sort {
direction := "ASC" direction := "ASC"
if strings.EqualFold(sort.Direction, "desc") { if strings.EqualFold(sort.Direction, "desc") {
@@ -640,9 +643,12 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
if strings.HasPrefix(sort.Column, "(") && strings.HasSuffix(sort.Column, ")") { if strings.HasPrefix(sort.Column, "(") && strings.HasSuffix(sort.Column, ")") {
// For expressions, pass as raw SQL to prevent auto-quoting // For expressions, pass as raw SQL to prevent auto-quoting
query = query.OrderExpr(fmt.Sprintf("%s %s", sort.Column, direction)) query = query.OrderExpr(fmt.Sprintf("%s %s", sort.Column, direction))
} else if strings.Contains(sort.Column, ".") {
// Already qualified (e.g. alias.column) - pass as raw expression to preserve the dot
query = query.OrderExpr(fmt.Sprintf("%s %s", sort.Column, direction))
} else { } else {
// Regular column - let Bun handle quoting // Unqualified column - prefix with main table alias to avoid ambiguity on JOINs
query = query.Order(fmt.Sprintf("%s %s", sort.Column, direction)) query = query.OrderExpr(fmt.Sprintf("%s.%s %s", common.QuoteIdent(tableAlias), common.QuoteIdent(sort.Column), direction))
} }
} }