mirror of
https://github.com/ent/ent.git
synced 2026-04-28 05:30:56 +03:00
entc/gen: support count with field selection
Fixed https://github.com/ent/ent/issues/2036
This commit is contained in:
committed by
Ariel Mashraki
parent
732b2a7d6e
commit
3e6c40886d
@@ -599,10 +599,20 @@ func (q *query) count(ctx context.Context, drv dialect.Driver) (int, error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
selector.Count(selector.C(q.Node.ID.Column))
|
||||
// If no columns were selected in count,
|
||||
// the default selection is by node ids.
|
||||
columns := q.Node.Columns
|
||||
if len(columns) == 0 {
|
||||
columns = append(columns, q.Node.ID.Column)
|
||||
}
|
||||
for i, c := range columns {
|
||||
columns[i] = selector.C(c)
|
||||
}
|
||||
if q.Unique {
|
||||
selector.SetDistinct(false)
|
||||
selector.Count(sql.Distinct(selector.C(q.Node.ID.Column)))
|
||||
selector.Count(sql.Distinct(columns...))
|
||||
} else {
|
||||
selector.Count(columns...)
|
||||
}
|
||||
query, args := selector.Query()
|
||||
if err := drv.Query(ctx, query, args, rows); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user