-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
Severity:
Description:
Using JOIN operations in COUNT(*) queries causes driver panic.
Symptoms:
- Runtime panic when executing COUNT with JOINs
- Application crashes
Minimal Reproduction:
func countOrganizations(db *sql.DB) (int, error) {
var count int
// This causes panic
query := `
SELECT COUNT(*)
FROM GPC.Organization o
LEFT JOIN GPC.Org_Type ot ON o.Org_Org_Type_ID = ot.Org_Type_ID
`
err := db.QueryRow(query).Scan(&count) // Panics here
return count, err
}Workaround:
Keep COUNT queries simple without JOINs:
func listOrganizations(db *sql.DB, page, pageSize int) ([]Organization, error) {
// Count query - no JOINs
var totalCount int
countQuery := `SELECT COUNT(*) FROM GPC.Organization`
db.QueryRow(countQuery).Scan(&totalCount)
// Main query with JOINs
offset := (page - 1) * pageSize
query := fmt.Sprintf(`
SELECT
o.Org_ID, o.Org_Name,
COALESCE(ot.Org_Type_Name, '')
FROM GPC.Organization o
LEFT JOIN GPC.Org_Type ot ON o.Org_Org_Type_ID = ot.Org_Type_ID
ORDER BY o.Org_Name
OFFSET %d ROWS FETCH NEXT %d ROWS ONLY
`, offset, pageSize)
rows, _ := db.Query(query)
// ... process results
return organizations, nil
}Impact: Medium - requires separating COUNT and main queries, complicates pagination logic.
Metadata
Metadata
Assignees
Labels
No labels