Jack Ling 612e545ae2 fix(databse/gdb): use COUNT(1) if fields number is greater than 1 even when parameter useFieldForCount is true in AllAndCount/ScanAndCount (#4701)
## Summary
Fix bug where `AllAndCount(true)` with multiple fields generates invalid
SQL `COUNT(field1, field2, ...)` causing syntax error.

## Root Cause
When `useFieldForCount=true`, the COUNT query inherits the fields
configuration from the model:
```go
// Before (buggy code)
if !useFieldForCount {
    countModel.fields = []any{Raw("1")}
}
// When useFieldForCount=true, fields remain as ["id", "nickname"]
// Generates: SELECT COUNT(id, nickname) FROM table 
```

## Fix
Always use `COUNT(1)` regardless of `useFieldForCount` parameter since
COUNT() accepts only one argument:
```go
// After (fixed code)
// Always use COUNT(1) for counting, regardless of useFieldForCount.
// COUNT() accepts only one argument, so we can't use multiple fields.
countModel.fields = []any{Raw("1")}
```

Applied to both `AllAndCount()` and `ScanAndCount()` methods.

## Tests
Added `Test_Issue4698` with 5 test cases:
1. AllAndCount(true) with multiple fields
2. AllAndCount(false) with multiple fields (baseline)
3. ScanAndCount with multiple fields
4. AllAndCount with single field
5. AllAndCount with WHERE condition

All tests verify that COUNT generates valid SQL and returns correct
count.

## Related
Fixes #4698
Ref #4703 (discovered during pagination test development)
2026-02-27 16:12:58 +08:00
2026-01-26 20:37:48 +08:00
dev
2017-07-04 18:31:39 +08:00
2025-12-27 16:07:23 +08:00
2026-01-26 20:37:48 +08:00

English | 简体中文

goframe logo

Go Reference GoFrame CI OpenSSF Scorecard CII Best Practices Go Report Card Code Coverage Production Ready License

Release GitHub pull requests GitHub closed pull requests GitHub issues GitHub closed issues Stars Forks Ask DeepWiki

A powerful framework for faster, easier, and more efficient project development.

Installation

go get -u github.com/gogf/gf/v2

Documentation

Contributors

💖 Thanks to all the contributors who made GoFrame possible 💖

goframe contributors

License

GoFrame is licensed under the MIT License, 100% free and open-source, forever.

Description
No description provided
Readme MIT 34 MiB
Languages
GO 100%