GROUP BY & HAVING (what to do instead)
fusion-orm does not implement Group()/Having() yet — here is what works today, and what does not.
Not implemented
fusion-orm does not implement Group() or Having() chainable methods.
docs/DESIGN_SYSTEM.md lists them among the gorm-compatible chain verbs it
aims for, but no such methods exist on *DB today. Anything you read
elsewhere showing a Group/Having chain on a *DB is not describing
this library.
This page is the honest replacement: what the gap is, which workarounds genuinely work, and one workaround that looks plausible but does not.
The gap
The design targets gorm's chain-verb surface — Model, Select, Where, Order, Limit, Offset, Joins, Preload, Group, Having, Distinct, Scopes, Clauses — but Group and Having are the two verbs not yet built:
Chain verbs | gorm-compatible names | Model, Select, Where, Order, Limit,
Offset, Joins, Preload, Group, Having, Distinct, Scopes, ClausesThere is no clause.Group builder and no GROUP BY slot in the clause
build order. Until both land, grouped aggregation needs one of the
alternatives below.
Works: Select an aggregate, Scan a scalar
For a global aggregate, project a raw aggregate expression and scan it
into a scalar. Select entries containing spaces or parentheses render
verbatim, so the function call survives intact:
var total int64
if err := db.Model(&crudUser{}).Select(`COUNT(*) AS total`).Scan(&total).Error; err != nil || total != 3 {
t.Errorf("scalar Scan = %d, %v", total, err)
}SELECT COUNT(*) AS total FROM "crud_users"The same shape covers SUM, MIN, MAX, and AVG, and composes with
Where — you get one row back, which is exactly the constraint: this
pattern cannot produce one row per group, because it cannot emit
GROUP BY.
Does not work: a raw "GROUP BY" fragment inside Select
It is tempting to smuggle GROUP BY through a Select column, since
columns with spaces render verbatim. It fails, and it is worth seeing why.
The statement builds SELECT before FROM:
"with", "select", "insert", "update", "delete", "from", "join", "set", "values",so a fragment like "age, COUNT(*) AS total GROUP BY age" renders inside
the column list, ahead of the table:
SELECT age, COUNT(*) AS total GROUP BY age FROM "crud_users"That is invalid SQL — GROUP BY must follow FROM/WHERE. The same
applies to Clauses(clause.Select{...}) and to any attempt to push the
keywords through Table (table names are identifier-quoted). Don't do
this; use one of the two working alternatives.
Works: fully raw SQL through the pool
For per-group aggregates, drop to database/sql via db.DB() — the
facade intentionally has no raw-read method on *DB, and the test suite's
own helper says so:
// scalarCount runs a raw scalar COUNT through the underlying pool (the
// facade intentionally has no Raw method). Lives in the default suite so both
// the promoted tests and the remaining audit-tagged tests can use it.Exec covers raw statements without rows; for raw reads, query the pool
directly:
pool, err := db.DB()
if err != nil {
return err
}
type ageTotal struct {
Age int
Total int64
}
rows, err := pool.QueryContext(ctx,
`SELECT age, COUNT(*) AS total FROM crud_users GROUP BY age ORDER BY age`)
if err != nil {
return err
}
defer rows.Close()
var out []ageTotal
for rows.Next() {
var t ageTotal
if err := rows.Scan(&t.Age, &t.Total); err != nil {
return err
}
out = append(out, t)
}
if err := rows.Err(); err != nil {
return err
}SELECT age, COUNT(*) AS total FROM crud_users GROUP BY age ORDER BY ageYou lose clause building, soft-delete injection, and the statement cache
for that one query — you write the SQL, you keep it correct. For a single
scalar it is one line: pool.QueryRowContext(ctx, query, args...).Scan(&n)
— the shape the test suite's own scalarCount helper uses. The chain-side
single-row path Row() builds from the statement, so it cannot carry raw
SQL either.
Track the gap, don't fake it
When Group/Having land, they will appear in the chainable-methods
reference first. Until then: scalar aggregates through Select + Scan,
per-group aggregates through the pool. If a snippet in the wild shows a
chainable Group(), it predates or ignores this page.